blob: a6c8a6e92c04b953c4dabaeaeaf14ad3b5d13d49 [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>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020038#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020039
Kees Cook144d1822013-02-18 12:02:32 -080040#ifdef HAVE_LINUX_MODULE_H
41#include <linux/module.h>
42#endif
43
Lucas De Marchi8f788d52011-11-25 01:22:56 -020044#include "libkmod.h"
Lucas De Marchi83b855a2013-07-04 16:13:11 -030045#include "libkmod-internal.h"
Lucas De Marchi8f788d52011-11-25 01:22:56 -020046
47/**
Lucas De Marchi66819512012-01-09 04:47:40 -020048 * SECTION:libkmod-module
49 * @short_description: operate on kernel modules
50 */
51
52/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020053 * kmod_module:
54 *
55 * Opaque object representing a module.
56 */
57struct kmod_module {
58 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020059 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020060 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020061 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020062 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020063 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020064 const char *install_commands; /* owned by kmod_config */
65 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020066 char *alias; /* only set if this module was created from an alias */
Lucas De Marchi1eff9422012-10-18 01:36:33 -030067 struct kmod_file *file;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020068 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020069 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020070 struct {
71 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020072 bool options : 1;
73 bool install_commands : 1;
74 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020075 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020076
77 /*
78 * private field used by kmod_module_get_probe_list() to detect
79 * dependency loops
80 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020081 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020082
83 /*
84 * set by kmod_module_get_probe_list: indicates for probe_insert()
85 * whether the module's command and softdep should be ignored
86 */
87 bool ignorecmd : 1;
Lucas De Marchi38052742012-02-16 20:43:16 -020088
89 /*
90 * if module was created by searching the modules.builtin file, this
91 * is set. There's nothing much useful one can do with such a
92 * "module", except knowing it's builtin.
93 */
94 bool builtin : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020095};
96
Lucas De Marchic35347f2011-12-12 10:48:02 -020097static inline const char *path_join(const char *path, size_t prefixlen,
98 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020099{
100 size_t pathlen;
101
102 if (path[0] == '/')
103 return path;
104
105 pathlen = strlen(path);
106 if (prefixlen + pathlen + 1 >= PATH_MAX)
107 return NULL;
108
109 memcpy(buf + prefixlen, path, pathlen + 1);
110 return buf;
111}
112
Dave Reisneraf9572c2012-02-02 11:07:33 -0500113static inline bool module_is_inkernel(struct kmod_module *mod)
114{
115 int state = kmod_module_get_initstate(mod);
Lucas De Marchi38052742012-02-16 20:43:16 -0200116
Dave Reisneraf9572c2012-02-02 11:07:33 -0500117 if (state == KMOD_MODULE_LIVE ||
Dave Reisneraf9572c2012-02-02 11:07:33 -0500118 state == KMOD_MODULE_BUILTIN)
119 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200120
121 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500122}
123
Lucas De Marchi671d4892011-12-05 20:23:05 -0200124int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200125{
126 struct kmod_ctx *ctx = mod->ctx;
127 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200128 const char *dirname;
129 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200130 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200131 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200132 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200133
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200134 if (mod->init.dep)
135 return mod->n_dep;
136 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200137 mod->init.dep = true;
138
139 p = strchr(line, ':');
140 if (p == NULL)
141 return 0;
142
Lucas De Marchi671d4892011-12-05 20:23:05 -0200143 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200144 dirname = kmod_get_dirname(mod->ctx);
145 dirnamelen = strlen(dirname);
146 if (dirnamelen + 2 >= PATH_MAX)
147 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200148
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200149 memcpy(buf, dirname, dirnamelen);
150 buf[dirnamelen] = '/';
151 dirnamelen++;
152 buf[dirnamelen] = '\0';
153
154 if (mod->path == NULL) {
155 const char *str = path_join(line, dirnamelen, buf);
156 if (str == NULL)
157 return 0;
158 mod->path = strdup(str);
159 if (mod->path == NULL)
160 return 0;
161 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200162
Lucas De Marchi7636e722011-12-01 17:56:03 -0200163 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200164 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
165 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200166 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200167 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200168
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200169 path = path_join(p, dirnamelen, buf);
170 if (path == NULL) {
171 ERR(ctx, "could not join path '%s' and '%s'.\n",
172 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200173 goto fail;
174 }
175
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200176 err = kmod_module_new_from_path(ctx, path, &depmod);
177 if (err < 0) {
178 ERR(ctx, "ctx=%p path=%s error=%s\n",
179 ctx, path, strerror(-err));
180 goto fail;
181 }
182
183 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200184
Lucas De Marchib94a7372011-12-26 20:10:49 -0200185 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200186 n++;
187 }
188
189 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
190
191 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200192 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200193 return n;
194
195fail:
196 kmod_module_unref_list(list);
197 mod->init.dep = false;
198 return err;
199}
200
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200201void kmod_module_set_visited(struct kmod_module *mod, bool visited)
202{
203 mod->visited = visited;
204}
205
Lucas De Marchi38052742012-02-16 20:43:16 -0200206void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
207{
208 mod->builtin = builtin;
209}
210
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200211/*
212 * Memory layout with alias:
213 *
214 * struct kmod_module {
215 * hashkey -----.
216 * alias -----. |
217 * name ----. | |
218 * } | | |
219 * name <----------' | |
220 * alias <-----------' |
221 * name\alias <--------'
222 *
223 * Memory layout without alias:
224 *
225 * struct kmod_module {
226 * hashkey ---.
227 * alias -----|----> NULL
228 * name ----. |
229 * } | |
230 * name <----------'-'
231 *
232 * @key is "name\alias" or "name" (in which case alias == NULL)
233 */
234static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
235 const char *name, size_t namelen,
236 const char *alias, size_t aliaslen,
237 struct kmod_module **mod)
238{
239 struct kmod_module *m;
240 size_t keylen;
241
242 m = kmod_pool_get_module(ctx, key);
243 if (m != NULL) {
244 *mod = kmod_module_ref(m);
245 return 0;
246 }
247
248 if (alias == NULL)
249 keylen = namelen;
250 else
251 keylen = namelen + aliaslen + 1;
252
253 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
Lucas De Marchi9f025612013-11-18 11:52:53 -0200254 if (m == NULL)
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200255 return -ENOMEM;
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200256
257 memset(m, 0, sizeof(*m));
258
259 m->ctx = kmod_ref(ctx);
260 m->name = (char *)m + sizeof(*m);
261 memcpy(m->name, key, keylen + 1);
262 if (alias == NULL) {
263 m->hashkey = m->name;
264 m->alias = NULL;
265 } else {
266 m->name[namelen] = '\0';
267 m->alias = m->name + namelen + 1;
268 m->hashkey = m->name + keylen + 1;
269 memcpy(m->hashkey, key, keylen + 1);
270 }
271
272 m->refcount = 1;
273 kmod_pool_add_module(ctx, m, m->hashkey);
274 *mod = m;
275
276 return 0;
277}
278
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200279/**
280 * kmod_module_new_from_name:
281 * @ctx: kmod library context
282 * @name: name of the module
283 * @mod: where to save the created struct kmod_module
284 *
285 * Create a new struct kmod_module using the module name. @name can not be an
286 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600287 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200288 *
289 * This function is also used internally by many others that return a new
290 * struct kmod_module or a new list of modules.
291 *
292 * The initial refcount is 1, and needs to be decremented to release the
293 * resources of the kmod_module. Since libkmod keeps track of all
294 * kmod_modules created, they are all released upon @ctx destruction too. Do
295 * not unref @ctx before all the desired operations with the returned
296 * kmod_module are done.
297 *
298 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
299 * module name or if memory allocation failed.
300 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200301KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
302 const char *name,
303 struct kmod_module **mod)
304{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200305 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200306 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200307
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200308 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200309 return -ENOENT;
310
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200311 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200312
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200313 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200314}
315
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200316int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
317 const char *name, struct kmod_module **mod)
318{
319 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200320 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200321 size_t namelen = strlen(name);
322 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200323
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200324 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200325 return -ENAMETOOLONG;
326
327 memcpy(key, name, namelen);
328 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200329 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200330
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200331 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200332 if (err < 0)
333 return err;
334
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200335 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200336}
337
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200338/**
339 * kmod_module_new_from_path:
340 * @ctx: kmod library context
341 * @path: path where to find the given module
342 * @mod: where to save the created struct kmod_module
343 *
344 * Create a new struct kmod_module using the module path. @path must be an
345 * existent file with in the filesystem and must be accessible to libkmod.
346 *
347 * The initial refcount is 1, and needs to be decremented to release the
348 * resources of the kmod_module. Since libkmod keeps track of all
349 * kmod_modules created, they are all released upon @ctx destruction too. Do
350 * not unref @ctx before all the desired operations with the returned
351 * kmod_module are done.
352 *
353 * If @path is relative, it's treated as relative to the current working
354 * directory. Otherwise, give an absolute path.
355 *
356 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
357 * it's not a valid file for a kmod_module or if memory allocation failed.
358 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200359KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
360 const char *path,
361 struct kmod_module **mod)
362{
363 struct kmod_module *m;
364 int err;
365 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200366 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200367 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200368 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200369
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200370 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200371 return -ENOENT;
372
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200373 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200374 if (abspath == NULL) {
375 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200376 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200377 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200378
379 err = stat(abspath, &st);
380 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200381 err = -errno;
382 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200383 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200384 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200385 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200386
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200387 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200388 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200389 free(abspath);
390 return -ENOENT;
391 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200392
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200393 m = kmod_pool_get_module(ctx, name);
394 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200395 if (m->path == NULL)
396 m->path = abspath;
397 else if (streq(m->path, abspath))
398 free(abspath);
399 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200400 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 -0200401 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200402 free(abspath);
403 return -EEXIST;
404 }
405
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200406 *mod = kmod_module_ref(m);
407 return 0;
408 }
409
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200410 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
411 if (err < 0)
412 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200413
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200414 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200415 *mod = m;
416
417 return 0;
418}
419
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200420/**
421 * kmod_module_unref:
422 * @mod: kmod module
423 *
424 * Drop a reference of the kmod module. If the refcount reaches zero, its
425 * resources are released.
426 *
427 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
428 * returns the passed @mod with its refcount decremented.
429 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200430KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
431{
432 if (mod == NULL)
433 return NULL;
434
435 if (--mod->refcount > 0)
436 return mod;
437
438 DBG(mod->ctx, "kmod_module %p released\n", mod);
439
Lucas De Marchi4084c172011-12-15 13:43:22 -0200440 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200441 kmod_module_unref_list(mod->dep);
Lucas De Marchi1eff9422012-10-18 01:36:33 -0300442
443 if (mod->file)
444 kmod_file_unref(mod->file);
445
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200446 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200447 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200448 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200449 free(mod);
450 return NULL;
451}
452
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200453/**
454 * kmod_module_ref:
455 * @mod: kmod module
456 *
457 * Take a reference of the kmod module, incrementing its refcount.
458 *
459 * Returns: the passed @module with its refcount incremented.
460 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200461KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
462{
463 if (mod == NULL)
464 return NULL;
465
466 mod->refcount++;
467
468 return mod;
469}
470
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200471#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
472 do { \
473 if ((_err) < 0) \
474 goto _label_err; \
475 if (*(_list) != NULL) \
476 goto finish; \
477 } while (0)
478
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200479/**
480 * kmod_module_new_from_lookup:
481 * @ctx: kmod library context
482 * @given_alias: alias to look for
483 * @list: an empty list where to save the list of modules matching
484 * @given_alias
485 *
486 * Create a new list of kmod modules using an alias or module name and lookup
487 * libkmod's configuration files and indexes in order to find the module.
488 * Once it's found in one of the places, it stops searching and create the
489 * list of modules that is saved in @list.
490 *
491 * The search order is: 1. aliases in configuration file; 2. module names in
492 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
493 * in modules.alias index.
494 *
495 * The initial refcount is 1, and needs to be decremented to release the
496 * resources of the kmod_module. The returned @list must be released by
497 * calling kmod_module_unref_list(). Since libkmod keeps track of all
498 * kmod_modules created, they are all released upon @ctx destruction too. Do
499 * not unref @ctx before all the desired operations with the returned list are
500 * completed.
501 *
502 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
503 * methods failed, which is basically due to memory allocation fail. If module
504 * is not found, it still returns 0, but @list is an empty list.
505 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200506KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200507 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200508 struct kmod_list **list)
509{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200510 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200511 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200512
Lucas De Marchi4308b172011-12-13 10:26:04 -0200513 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200514 return -ENOENT;
515
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200516 if (list == NULL || *list != NULL) {
517 ERR(ctx, "An empty list is needed to create lookup\n");
518 return -ENOSYS;
519 }
520
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200521 if (alias_normalize(given_alias, alias, NULL) < 0) {
522 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200523 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200524 }
525
526 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200527
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200528 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200529 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200530 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200531
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200532 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200533 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
534 CHECK_ERR_AND_FINISH(err, fail, list, finish);
535
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200536 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200537 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
538 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200539
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200540 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200541 err = kmod_lookup_alias_from_commands(ctx, alias, list);
542 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200543
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200544 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200545 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
546 CHECK_ERR_AND_FINISH(err, fail, list, finish);
547
Lucas De Marchi38052742012-02-16 20:43:16 -0200548 DBG(ctx, "lookup modules.builtin %s\n", alias);
549 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
550 CHECK_ERR_AND_FINISH(err, fail, list, finish);
551
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200552finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200553 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200554 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200555fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200556 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200557 kmod_module_unref_list(*list);
558 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200559 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200560}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200561#undef CHECK_ERR_AND_FINISH
562
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200563/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200564 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200565 * @list: list of kmod modules
566 *
567 * Drop a reference of each kmod module in @list and releases the resources
568 * taken by the list itself.
569 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800570 * Returns: 0
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200571 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200572KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
573{
574 for (; list != NULL; list = kmod_list_remove(list))
575 kmod_module_unref(list->data);
576
577 return 0;
578}
579
Lucas De Marchi0d467432011-12-31 11:15:52 -0200580/**
581 * kmod_module_get_filtered_blacklist:
582 * @ctx: kmod library context
583 * @input: list of kmod_module to be filtered with blacklist
584 * @output: where to save the new list
585 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200586 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500587 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200588 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500589 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200590 *
591 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
592 * list.
593 */
594KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
595 const struct kmod_list *input,
596 struct kmod_list **output)
597{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500598 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200599}
600
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200601static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
602{
603 if (!mod->init.dep) {
604 /* lazy init */
605 char *line = kmod_search_moddep(mod->ctx, mod->name);
606
607 if (line == NULL)
608 return NULL;
609
610 kmod_module_parse_depline((struct kmod_module *)mod, line);
611 free(line);
612
613 if (!mod->init.dep)
614 return NULL;
615 }
616
617 return mod->dep;
618}
619
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200620/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200621 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200622 * @mod: kmod module
623 *
624 * Search the modules.dep index to find the dependencies of the given @mod.
625 * The result is cached in @mod, so subsequent calls to this function will
626 * return the already searched list of modules.
627 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800628 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
629 * that can be released by calling kmod_module_unref_list().
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200630 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200631KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200632{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200633 struct kmod_list *l, *l_new, *list_new = NULL;
634
635 if (mod == NULL)
636 return NULL;
637
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200638 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200639
640 kmod_list_foreach(l, mod->dep) {
641 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
642 if (l_new == NULL) {
643 kmod_module_unref(l->data);
644 goto fail;
645 }
646
647 list_new = l_new;
648 }
649
650 return list_new;
651
652fail:
653 ERR(mod->ctx, "out of memory\n");
654 kmod_module_unref_list(list_new);
655 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200656}
657
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200658/**
659 * kmod_module_get_module:
660 * @entry: an entry in a list of kmod modules.
661 *
662 * Get the kmod module of this @entry in the list, increasing its refcount.
663 * After it's used, unref it. Since the refcount is incremented upon return,
664 * you still have to call kmod_module_unref_list() to release the list of kmod
665 * modules.
666 *
667 * Returns: NULL on failure or the kmod_module contained in this list entry
668 * with its refcount incremented.
669 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200670KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200671{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200672 if (entry == NULL)
673 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200674
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200675 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200676}
677
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200678/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200679 * kmod_module_get_name:
680 * @mod: kmod module
681 *
682 * Get the name of this kmod module. Name is always available, independently
683 * if it was created by kmod_module_new_from_name() or another function and
684 * it's always normalized (dashes are replaced with underscores).
685 *
686 * Returns: the name of this kmod module.
687 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200688KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200689{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200690 if (mod == NULL)
691 return NULL;
692
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200693 return mod->name;
694}
695
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200696/**
697 * kmod_module_get_path:
698 * @mod: kmod module
699 *
700 * Get the path of this kmod module. If this kmod module was not created by
701 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200702 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200703 *
704 * Returns: the path of this kmod module or NULL if such information is not
705 * available.
706 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200707KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200708{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200709 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200710
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200711 if (mod == NULL)
712 return NULL;
713
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200714 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200715
Lucas De Marchie005fac2011-12-08 10:42:34 -0200716 if (mod->path != NULL)
717 return mod->path;
718 if (mod->init.dep)
719 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200720
Lucas De Marchie005fac2011-12-08 10:42:34 -0200721 /* lazy init */
722 line = kmod_search_moddep(mod->ctx, mod->name);
723 if (line == NULL)
724 return NULL;
725
726 kmod_module_parse_depline((struct kmod_module *) mod, line);
727 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200728
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200729 return mod->path;
730}
731
732
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200733extern long delete_module(const char *name, unsigned int flags);
734
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200735/**
736 * kmod_module_remove_module:
737 * @mod: kmod module
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500738 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
Chengwei Yangd7152f62013-05-04 17:07:03 +0800739 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
740 * use by a kernel subsystem or other process;
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500741 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
742 * delete_module(2).
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200743 *
744 * Remove a module from Linux kernel.
745 *
746 * Returns: 0 on success or < 0 on failure.
747 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200748KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
749 unsigned int flags)
750{
751 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200752
753 if (mod == NULL)
754 return -ENOENT;
755
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500756 /* Filter out other flags and force ONONBLOCK */
757 flags &= KMOD_REMOVE_FORCE;
758 flags |= KMOD_REMOVE_NOWAIT;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200759
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200760 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200761 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200762 err = -errno;
763 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200764 }
765
Lucas De Marchiba998b92012-01-11 00:08:14 -0200766 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200767}
768
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200769extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200770
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200771/**
772 * kmod_module_insert_module:
773 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200774 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +0800775 * behavior of this function, valid flags are
776 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
777 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200778 * @options: module's options to pass to Linux Kernel.
779 *
780 * Insert a module in Linux kernel. It opens the file pointed by @mod,
781 * mmap'ing it and passing to kernel.
782 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200783 * Returns: 0 on success or < 0 on failure. If module is already loaded it
784 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200785 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200786KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200787 unsigned int flags,
788 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200789{
790 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200791 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200792 off_t size;
Michal Marekc2f4d852014-02-28 13:05:32 +0100793 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200794 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200795 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200796
797 if (mod == NULL)
798 return -ENOENT;
799
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200800 path = kmod_module_get_path(mod);
801 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500802 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200803 return -ENOSYS;
804 }
805
Michal Marekc2f4d852014-02-28 13:05:32 +0100806 mod->file = kmod_file_open(mod->ctx, path);
807 if (mod->file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200808 err = -errno;
809 return err;
810 }
811
Michal Marekc2f4d852014-02-28 13:05:32 +0100812 if (kmod_file_get_direct(mod->file)) {
Kees Cook144d1822013-02-18 12:02:32 -0800813 unsigned int kernel_flags = 0;
814
Kees Cook144d1822013-02-18 12:02:32 -0800815 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
816 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
817 if (flags & KMOD_INSERT_FORCE_MODVERSION)
818 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
Kees Cook144d1822013-02-18 12:02:32 -0800819
Michal Marekc2f4d852014-02-28 13:05:32 +0100820 err = finit_module(kmod_file_get_fd(mod->file), args, kernel_flags);
Kees Cook144d1822013-02-18 12:02:32 -0800821 if (err == 0 || errno != ENOSYS)
822 goto init_finished;
823 }
824
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200825 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
Michal Marekc2f4d852014-02-28 13:05:32 +0100826 elf = kmod_file_get_elf(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200827 if (elf == NULL) {
828 err = -errno;
Michal Marekc2f4d852014-02-28 13:05:32 +0100829 return err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200830 }
831
832 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
833 err = kmod_elf_strip_section(elf, "__versions");
834 if (err < 0)
835 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
836 }
837
838 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
839 err = kmod_elf_strip_vermagic(elf);
840 if (err < 0)
841 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
842 }
843
844 mem = kmod_elf_get_memory(elf);
Michal Marekc2f4d852014-02-28 13:05:32 +0100845 } else {
846 mem = kmod_file_get_contents(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200847 }
Michal Marekc2f4d852014-02-28 13:05:32 +0100848 size = kmod_file_get_size(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200849
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200850 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800851init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200852 if (err < 0) {
853 err = -errno;
854 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
855 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200856 return err;
857}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200858
Lucas De Marchiddbda022011-12-27 11:40:10 -0200859static bool module_is_blacklisted(struct kmod_module *mod)
860{
861 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300862 const struct kmod_config *config = kmod_get_config(ctx);
863 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200864 const struct kmod_list *l;
865
866 kmod_list_foreach(l, bl) {
867 const char *modname = kmod_blacklist_get_modname(l);
868
869 if (streq(modname, mod->name))
870 return true;
871 }
872
873 return false;
874}
875
Dave Reisnerd80b1032012-02-24 10:05:11 -0500876/**
877 * kmod_module_apply_filter
878 * @ctx: kmod library context
Chengwei Yangd7152f62013-05-04 17:07:03 +0800879 * @filter_type: bitmask to filter modules out, valid types are
880 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
881 * KMOD_FILTER_BUILTIN: filter builtin modules out.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500882 * @input: list of kmod_module to be filtered
883 * @output: where to save the new list
884 *
885 * Given a list @input, this function filter it out by the filter mask
886 * and save it in @output.
887 *
888 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
889 * list.
890 */
891KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
892 enum kmod_filter filter_type,
893 const struct kmod_list *input,
894 struct kmod_list **output)
895{
896 const struct kmod_list *li;
897
898 if (ctx == NULL || output == NULL)
899 return -ENOENT;
900
901 *output = NULL;
902 if (input == NULL)
903 return 0;
904
905 kmod_list_foreach(li, input) {
906 struct kmod_module *mod = li->data;
907 struct kmod_list *node;
908
909 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
910 module_is_blacklisted(mod))
911 continue;
912
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500913 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500914 continue;
915
916 node = kmod_list_append(*output, mod);
917 if (node == NULL)
918 goto fail;
919
920 *output = node;
921 kmod_module_ref(mod);
922 }
923
924 return 0;
925
926fail:
927 kmod_module_unref_list(*output);
928 *output = NULL;
929 return -ENOMEM;
930}
931
Lucas De Marchiddbda022011-12-27 11:40:10 -0200932static int command_do(struct kmod_module *mod, const char *type,
933 const char *cmd)
934{
935 const char *modname = kmod_module_get_name(mod);
936 int err;
937
938 DBG(mod->ctx, "%s %s\n", type, cmd);
939
940 setenv("MODPROBE_MODULE", modname, 1);
941 err = system(cmd);
942 unsetenv("MODPROBE_MODULE");
943
944 if (err == -1 || WEXITSTATUS(err)) {
945 ERR(mod->ctx, "Error running %s command for %s\n",
946 type, modname);
947 if (err != -1)
948 err = -WEXITSTATUS(err);
949 }
950
951 return err;
952}
953
Lucas De Marchib1a51252012-01-29 01:49:09 -0200954struct probe_insert_cb {
955 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
956 void *data;
957};
958
Lucas De Marchiddbda022011-12-27 11:40:10 -0200959static int module_do_install_commands(struct kmod_module *mod,
960 const char *options,
961 struct probe_insert_cb *cb)
962{
963 const char *command = kmod_module_get_install_commands(mod);
Lucas De Marchi9f025612013-11-18 11:52:53 -0200964 char *p;
965 _cleanup_free_ char *cmd;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200966 int err;
967 size_t cmdlen, options_len, varlen;
968
969 assert(command);
970
971 if (options == NULL)
972 options = "";
973
974 options_len = strlen(options);
975 cmdlen = strlen(command);
976 varlen = sizeof("$CMDLINE_OPTS") - 1;
977
978 cmd = memdup(command, cmdlen + 1);
979 if (cmd == NULL)
980 return -ENOMEM;
981
982 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
983 size_t prefixlen = p - cmd;
984 size_t suffixlen = cmdlen - prefixlen - varlen;
985 size_t slen = cmdlen - varlen + options_len;
986 char *suffix = p + varlen;
987 char *s = malloc(slen + 1);
Lucas De Marchi9f025612013-11-18 11:52:53 -0200988 if (!s)
Lucas De Marchiddbda022011-12-27 11:40:10 -0200989 return -ENOMEM;
Lucas De Marchi9f025612013-11-18 11:52:53 -0200990
Lucas De Marchiddbda022011-12-27 11:40:10 -0200991 memcpy(s, cmd, p - cmd);
992 memcpy(s + prefixlen, options, options_len);
993 memcpy(s + prefixlen + options_len, suffix, suffixlen);
994 s[slen] = '\0';
995
996 free(cmd);
997 cmd = s;
998 cmdlen = slen;
999 }
1000
1001 if (cb->run_install != NULL)
1002 err = cb->run_install(mod, cmd, cb->data);
1003 else
1004 err = command_do(mod, "install", cmd);
1005
Lucas De Marchiddbda022011-12-27 11:40:10 -02001006 return err;
1007}
1008
Lucas De Marchiddbda022011-12-27 11:40:10 -02001009static char *module_options_concat(const char *opt, const char *xopt)
1010{
1011 // TODO: we might need to check if xopt overrides options on opt
1012 size_t optlen = opt == NULL ? 0 : strlen(opt);
1013 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1014 char *r;
1015
1016 if (optlen == 0 && xoptlen == 0)
1017 return NULL;
1018
1019 r = malloc(optlen + xoptlen + 2);
1020
1021 if (opt != NULL) {
1022 memcpy(r, opt, optlen);
1023 r[optlen] = ' ';
1024 optlen++;
1025 }
1026
1027 if (xopt != NULL)
1028 memcpy(r + optlen, xopt, xoptlen);
1029
1030 r[optlen + xoptlen] = '\0';
1031
1032 return r;
1033}
1034
Lucas De Marchib1a51252012-01-29 01:49:09 -02001035static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001036 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001037 struct kmod_list **list);
1038
1039/* re-entrant */
1040static int __kmod_module_fill_softdep(struct kmod_module *mod,
1041 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001042{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001043 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001044 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001045
1046 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001047 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001048 ERR(mod->ctx, "could not get softdep: %s\n",
1049 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001050 goto fail;
1051 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001052
Lucas De Marchib1a51252012-01-29 01:49:09 -02001053 kmod_list_foreach(l, pre) {
1054 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001055 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001056 if (err < 0)
1057 goto fail;
1058 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001059
Lucas De Marchib1a51252012-01-29 01:49:09 -02001060 l = kmod_list_append(*list, kmod_module_ref(mod));
1061 if (l == NULL) {
1062 kmod_module_unref(mod);
1063 err = -ENOMEM;
1064 goto fail;
1065 }
1066 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001067 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001068
Lucas De Marchib1a51252012-01-29 01:49:09 -02001069 kmod_list_foreach(l, post) {
1070 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001071 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001072 if (err < 0)
1073 goto fail;
1074 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001075
Lucas De Marchib1a51252012-01-29 01:49:09 -02001076fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001077 kmod_module_unref_list(pre);
1078 kmod_module_unref_list(post);
1079
1080 return err;
1081}
1082
Lucas De Marchib1a51252012-01-29 01:49:09 -02001083/* re-entrant */
1084static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001085 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001086 struct kmod_list **list)
1087{
1088 struct kmod_list *dep, *l;
1089 int err = 0;
1090
1091 if (mod->visited) {
1092 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1093 mod->name);
1094 return 0;
1095 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001096 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001097
1098 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001099 kmod_list_foreach(l, dep) {
1100 struct kmod_module *m = l->data;
1101 err = __kmod_module_fill_softdep(m, list);
1102 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001103 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001104 }
1105
Lucas De Marchi89e92482012-01-29 02:35:46 -02001106 if (ignorecmd) {
1107 l = kmod_list_append(*list, kmod_module_ref(mod));
1108 if (l == NULL) {
1109 kmod_module_unref(mod);
1110 err = -ENOMEM;
1111 goto finish;
1112 }
1113 *list = l;
1114 mod->ignorecmd = true;
1115 } else
1116 err = __kmod_module_fill_softdep(mod, list);
1117
Lucas De Marchib1a51252012-01-29 01:49:09 -02001118finish:
1119 kmod_module_unref_list(dep);
1120 return err;
1121}
1122
1123static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001124 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001125 struct kmod_list **list)
1126{
1127 int err;
1128
1129 assert(mod != NULL);
1130 assert(list != NULL && *list == NULL);
1131
1132 /*
1133 * Make sure we don't get screwed by previous calls to this function
1134 */
1135 kmod_set_modules_visited(mod->ctx, false);
1136
Lucas De Marchi89e92482012-01-29 02:35:46 -02001137 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001138 if (err < 0) {
1139 kmod_module_unref_list(*list);
1140 *list = NULL;
1141 }
1142
1143 return err;
1144}
1145
Lucas De Marchiddbda022011-12-27 11:40:10 -02001146/**
1147 * kmod_module_probe_insert_module:
1148 * @mod: kmod module
1149 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +08001150 * behavior of this function, valid flags are
1151 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1152 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1153 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1154 * commands and softdeps configured in the system;
1155 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1156 * live in kernel or not;
1157 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1158 * associated callback function;
1159 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1160 * and the module is already live in kernel, the function will fail if this
1161 * flag is specified;
1162 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1163 * filter to this module and its dependencies. If any of the dependencies (or
1164 * the module) is blacklisted, the probe will fail, unless the blacklisted
1165 * module is already live in kernel;
1166 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1167 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1168 * alias and is blacklisted.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001169 * @extra_options: module's options to pass to Linux Kernel. It applies only
1170 * to @mod, not to its dependencies.
1171 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001172 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001173 * @print_action: function to call with the action being taken (install or
1174 * insmod). It's useful for tools like modprobe when running with verbose
1175 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001176 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001177 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001178 * install commands and applying blacklist.
1179 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001180 * If @run_install is NULL, this function will fork and exec by calling
1181 * system(3). Don't pass a NULL argument in @run_install if your binary is
1182 * setuid/setgid (see warning in system(3)). If you need control over the
1183 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001184 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001185 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1186 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001187 */
1188KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1189 unsigned int flags, const char *extra_options,
1190 int (*run_install)(struct kmod_module *m,
1191 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001192 const void *data,
1193 void (*print_action)(struct kmod_module *m,
1194 bool install,
1195 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001196{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001197 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001198 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001199 int err;
1200
1201 if (mod == NULL)
1202 return -ENOENT;
1203
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001204 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1205 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001206 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001207 return -EEXIST;
1208 else
1209 return 0;
1210 }
1211
Lucas De Marchi68820172012-08-17 09:38:05 -03001212 /*
1213 * Ugly assignement + check. We need to check if we were told to check
1214 * blacklist and also return the reason why we failed.
1215 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1216 * module is an alias, so we also need to check it
1217 */
1218 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1219 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1220 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001221 if (module_is_blacklisted(mod))
1222 return err;
1223 }
1224
Lucas De Marchi89e92482012-01-29 02:35:46 -02001225 err = kmod_module_get_probe_list(mod,
1226 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001227 if (err < 0)
1228 return err;
1229
1230 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1231 struct kmod_list *filtered = NULL;
1232
Dave Reisnerd80b1032012-02-24 10:05:11 -05001233 err = kmod_module_apply_filter(mod->ctx,
1234 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001235 if (err < 0)
1236 return err;
1237
1238 kmod_module_unref_list(list);
1239 if (filtered == NULL)
1240 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1241
1242 list = filtered;
1243 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001244
1245 cb.run_install = run_install;
1246 cb.data = (void *) data;
1247
Lucas De Marchib1a51252012-01-29 01:49:09 -02001248 kmod_list_foreach(l, list) {
1249 struct kmod_module *m = l->data;
1250 const char *moptions = kmod_module_get_options(m);
1251 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001252 char *options;
1253
1254 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1255 && module_is_inkernel(m)) {
1256 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1257 m->name);
1258 err = -EEXIST;
1259 goto finish_module;
1260 }
1261
1262 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001263 m == mod ? extra_options : NULL);
1264
Lucas De Marchi89e92482012-01-29 02:35:46 -02001265 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001266 if (print_action != NULL)
1267 print_action(m, true, options ?: "");
1268
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001269 if (!(flags & KMOD_PROBE_DRY_RUN))
1270 err = module_do_install_commands(m, options,
1271 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001272 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001273 if (print_action != NULL)
1274 print_action(m, false, options ?: "");
1275
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001276 if (!(flags & KMOD_PROBE_DRY_RUN))
1277 err = kmod_module_insert_module(m, flags,
1278 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001279 }
1280
1281 free(options);
1282
Lucas De Marchiabd55572012-02-19 04:20:30 -02001283finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001284 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001285 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001286 * already loaded and the module being loaded is not a softdep
1287 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001288 *
1289 * We need to check here because of race conditions. We
1290 * checked first if module was already loaded but it may have
1291 * been loaded between the check and the moment we try to
1292 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001293 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001294 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001295 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001296 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001297
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001298 if (err == -EEXIST)
1299 err = 0;
1300 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001301 break;
1302 }
1303
1304 kmod_module_unref_list(list);
1305 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001306}
1307
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001308/**
1309 * kmod_module_get_options:
1310 * @mod: kmod module
1311 *
1312 * Get options of this kmod module. Options come from the configuration file
1313 * and are cached in @mod. The first call to this function will search for
1314 * this module in configuration and subsequent calls return the cached string.
1315 *
1316 * Returns: a string with all the options separated by spaces. This string is
1317 * owned by @mod, do not free it.
1318 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001319KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1320{
1321 if (mod == NULL)
1322 return NULL;
1323
1324 if (!mod->init.options) {
1325 /* lazy init */
1326 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001327 const struct kmod_list *l;
1328 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001329 char *opts = NULL;
1330 size_t optslen = 0;
1331
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001332 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001333
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001334 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001335 const char *modname = kmod_option_get_modname(l);
1336 const char *str;
1337 size_t len;
1338 void *tmp;
1339
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001340 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1341 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1342 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001343 continue;
1344
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001345 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 -02001346 str = kmod_option_get_options(l);
1347 len = strlen(str);
1348 if (len < 1)
1349 continue;
1350
1351 tmp = realloc(opts, optslen + len + 2);
1352 if (tmp == NULL) {
1353 free(opts);
1354 goto failed;
1355 }
1356
1357 opts = tmp;
1358
1359 if (optslen > 0) {
1360 opts[optslen] = ' ';
1361 optslen++;
1362 }
1363
1364 memcpy(opts + optslen, str, len);
1365 optslen += len;
1366 opts[optslen] = '\0';
1367 }
1368
1369 m->init.options = true;
1370 m->options = opts;
1371 }
1372
1373 return mod->options;
1374
1375failed:
1376 ERR(mod->ctx, "out of memory\n");
1377 return NULL;
1378}
1379
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001380/**
1381 * kmod_module_get_install_commands:
1382 * @mod: kmod module
1383 *
1384 * Get install commands for this kmod module. Install commands come from the
1385 * configuration file and are cached in @mod. The first call to this function
1386 * will search for this module in configuration and subsequent calls return
1387 * the cached string. The install commands are returned as they were in the
1388 * configuration, concatenated by ';'. No other processing is made in this
1389 * string.
1390 *
1391 * Returns: a string with all install commands separated by semicolons. This
1392 * string is owned by @mod, do not free it.
1393 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001394KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1395{
1396 if (mod == NULL)
1397 return NULL;
1398
1399 if (!mod->init.install_commands) {
1400 /* lazy init */
1401 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001402 const struct kmod_list *l;
1403 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001404
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001405 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001406
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001407 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001408 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001409
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001410 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001411 continue;
1412
Lucas De Marchi60f67602011-12-16 03:33:26 -02001413 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001414
Lucas De Marchi60f67602011-12-16 03:33:26 -02001415 /*
1416 * find only the first command, as modprobe from
1417 * module-init-tools does
1418 */
1419 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001420 }
1421
1422 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001423 }
1424
1425 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001426}
1427
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001428void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1429{
1430 mod->init.install_commands = true;
1431 mod->install_commands = cmd;
1432}
1433
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001434static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1435{
1436 struct kmod_list *ret = NULL;
1437 unsigned i;
1438
1439 for (i = 0; i < count; i++) {
1440 const char *depname = array[i];
1441 struct kmod_list *lst = NULL;
1442 int err;
1443
1444 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1445 if (err < 0) {
1446 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1447 continue;
1448 } else if (lst != NULL)
1449 ret = kmod_list_append_list(ret, lst);
1450 }
1451 return ret;
1452}
1453
1454/**
1455 * kmod_module_get_softdeps:
1456 * @mod: kmod module
1457 * @pre: where to save the list of preceding soft dependencies.
1458 * @post: where to save the list of post soft dependencies.
1459 *
1460 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001461 * from configuration file and are not cached in @mod because it may include
1462 * dependency cycles that would make we leak kmod_module. Any call
1463 * to this function will search for this module in configuration, allocate a
1464 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001465 *
1466 * Both @pre and @post are newly created list of kmod_module and
1467 * should be unreferenced with kmod_module_unref_list().
1468 *
1469 * Returns: 0 on success or < 0 otherwise.
1470 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001471KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1472 struct kmod_list **pre,
1473 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001474{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001475 const struct kmod_list *l;
1476 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001477
1478 if (mod == NULL || pre == NULL || post == NULL)
1479 return -ENOENT;
1480
1481 assert(*pre == NULL);
1482 assert(*post == NULL);
1483
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001484 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001485
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001486 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001487 const char *modname = kmod_softdep_get_name(l);
1488 const char * const *array;
1489 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001490
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001491 if (fnmatch(modname, mod->name, 0) != 0)
1492 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001493
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001494 array = kmod_softdep_get_pre(l, &count);
1495 *pre = lookup_softdep(mod->ctx, array, count);
1496 array = kmod_softdep_get_post(l, &count);
1497 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001498
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001499 /*
1500 * find only the first command, as modprobe from
1501 * module-init-tools does
1502 */
1503 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001504 }
1505
1506 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001507}
1508
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001509/**
1510 * kmod_module_get_remove_commands:
1511 * @mod: kmod module
1512 *
1513 * Get remove commands for this kmod module. Remove commands come from the
1514 * configuration file and are cached in @mod. The first call to this function
1515 * will search for this module in configuration and subsequent calls return
1516 * the cached string. The remove commands are returned as they were in the
1517 * configuration, concatenated by ';'. No other processing is made in this
1518 * string.
1519 *
1520 * Returns: a string with all remove commands separated by semicolons. This
1521 * string is owned by @mod, do not free it.
1522 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001523KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1524{
1525 if (mod == NULL)
1526 return NULL;
1527
1528 if (!mod->init.remove_commands) {
1529 /* lazy init */
1530 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001531 const struct kmod_list *l;
1532 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001533
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001534 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001535
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001536 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001537 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001538
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001539 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001540 continue;
1541
Lucas De Marchi60f67602011-12-16 03:33:26 -02001542 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001543
Lucas De Marchi60f67602011-12-16 03:33:26 -02001544 /*
1545 * find only the first command, as modprobe from
1546 * module-init-tools does
1547 */
1548 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001549 }
1550
1551 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001552 }
1553
1554 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001555}
1556
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001557void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1558{
1559 mod->init.remove_commands = true;
1560 mod->remove_commands = cmd;
1561}
1562
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001563/**
1564 * SECTION:libkmod-loaded
1565 * @short_description: currently loaded modules
1566 *
1567 * Information about currently loaded modules, as reported by Linux kernel.
1568 * These information are not cached by libkmod and are always read from /sys
1569 * and /proc/modules.
1570 */
1571
1572/**
1573 * kmod_module_new_from_loaded:
1574 * @ctx: kmod library context
1575 * @list: where to save the list of loaded modules
1576 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001577 * Create a new list of kmod modules with all modules currently loaded in
1578 * kernel. It uses /proc/modules to get the names of loaded modules and to
1579 * create kmod modules by calling kmod_module_new_from_name() in each of them.
Chengwei Yang491c4902013-05-04 17:07:02 +08001580 * They are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001581 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001582 * The initial refcount is 1, and needs to be decremented to release the
1583 * resources of the kmod_module. The returned @list must be released by
1584 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1585 * kmod_modules created, they are all released upon @ctx destruction too. Do
1586 * not unref @ctx before all the desired operations with the returned list are
1587 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001588 *
1589 * Returns: 0 on success or < 0 on error.
1590 */
1591KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1592 struct kmod_list **list)
1593{
1594 struct kmod_list *l = NULL;
1595 FILE *fp;
1596 char line[4096];
1597
1598 if (ctx == NULL || list == NULL)
1599 return -ENOENT;
1600
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001601 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001602 if (fp == NULL) {
1603 int err = -errno;
1604 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1605 return err;
1606 }
1607
1608 while (fgets(line, sizeof(line), fp)) {
1609 struct kmod_module *m;
1610 struct kmod_list *node;
1611 int err;
1612 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1613
1614 err = kmod_module_new_from_name(ctx, name, &m);
1615 if (err < 0) {
1616 ERR(ctx, "could not get module from name '%s': %s\n",
1617 name, strerror(-err));
1618 continue;
1619 }
1620
1621 node = kmod_list_append(l, m);
1622 if (node)
1623 l = node;
1624 else {
1625 ERR(ctx, "out of memory\n");
1626 kmod_module_unref(m);
1627 }
1628 }
1629
1630 fclose(fp);
1631 *list = l;
1632
1633 return 0;
1634}
1635
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001636/**
1637 * kmod_module_initstate_str:
1638 * @state: the state as returned by kmod_module_get_initstate()
1639 *
1640 * Translate a initstate to a string.
1641 *
1642 * Returns: the string associated to the @state. This string is statically
1643 * allocated, do not free it.
1644 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001645KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1646{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001647 switch (state) {
1648 case KMOD_MODULE_BUILTIN:
1649 return "builtin";
1650 case KMOD_MODULE_LIVE:
1651 return "live";
1652 case KMOD_MODULE_COMING:
1653 return "coming";
1654 case KMOD_MODULE_GOING:
1655 return "going";
1656 default:
1657 return NULL;
1658 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001659}
1660
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001661/**
1662 * kmod_module_get_initstate:
1663 * @mod: kmod module
1664 *
1665 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1666 * /sys filesystem.
1667 *
Chengwei Yangd7152f62013-05-04 17:07:03 +08001668 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1669 * KMOD_MODULE_BUILTIN: module is builtin;
1670 * KMOD_MODULE_LIVE: module is live in kernel;
1671 * KMOD_MODULE_COMING: module is being loaded;
1672 * KMOD_MODULE_GOING: module is being unloaded.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001673 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001674KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1675{
1676 char path[PATH_MAX], buf[32];
1677 int fd, err, pathlen;
1678
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001679 if (mod == NULL)
1680 return -ENOENT;
1681
Lucas De Marchi38052742012-02-16 20:43:16 -02001682 if (mod->builtin)
1683 return KMOD_MODULE_BUILTIN;
1684
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001685 pathlen = snprintf(path, sizeof(path),
1686 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001687 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001688 if (fd < 0) {
1689 err = -errno;
1690
Lucas De Marchiddbda022011-12-27 11:40:10 -02001691 DBG(mod->ctx, "could not open '%s': %s\n",
1692 path, strerror(-err));
1693
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001694 if (pathlen > (int)sizeof("/initstate") - 1) {
1695 struct stat st;
1696 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1697 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1698 return KMOD_MODULE_BUILTIN;
1699 }
1700
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001701 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001702 path, strerror(-err));
1703 return err;
1704 }
1705
1706 err = read_str_safe(fd, buf, sizeof(buf));
1707 close(fd);
1708 if (err < 0) {
1709 ERR(mod->ctx, "could not read from '%s': %s\n",
1710 path, strerror(-err));
1711 return err;
1712 }
1713
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001714 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001715 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001716 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001717 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001718 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001719 return KMOD_MODULE_GOING;
1720
1721 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1722 return -EINVAL;
1723}
1724
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001725/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001726 * kmod_module_get_size:
1727 * @mod: kmod module
1728 *
Dave Reisner486f9012012-06-28 09:09:48 -04001729 * Get the size of this kmod module as returned by Linux kernel. If supported,
1730 * the size is read from the coresize attribute in /sys/module. For older
1731 * kernels, this falls back on /proc/modules and searches for the specified
1732 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001733 *
1734 * Returns: the size of this kmod module.
1735 */
1736KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1737{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001738 FILE *fp;
1739 char line[4096];
1740 int lineno = 0;
1741 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001742 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001743
1744 if (mod == NULL)
1745 return -ENOENT;
1746
Dave Reisner486f9012012-06-28 09:09:48 -04001747 /* try to open the module dir in /sys. If this fails, don't
1748 * bother trying to find the size as we know the module isn't
1749 * loaded.
1750 */
1751 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1752 dfd = open(line, O_RDONLY);
1753 if (dfd < 0)
1754 return -errno;
1755
1756 /* available as of linux 3.3.x */
1757 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1758 if (cfd >= 0) {
1759 if (read_str_long(cfd, &size, 10) < 0)
1760 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1761 close(cfd);
1762 goto done;
1763 }
1764
1765 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001766 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001767 if (fp == NULL) {
1768 int err = -errno;
1769 ERR(mod->ctx,
1770 "could not open /proc/modules: %s\n", strerror(errno));
1771 return err;
1772 }
1773
1774 while (fgets(line, sizeof(line), fp)) {
1775 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1776 long value;
1777
1778 lineno++;
1779 if (tok == NULL || !streq(tok, mod->name))
1780 continue;
1781
1782 tok = strtok_r(NULL, " \t", &saveptr);
1783 if (tok == NULL) {
1784 ERR(mod->ctx,
1785 "invalid line format at /proc/modules:%d\n", lineno);
1786 break;
1787 }
1788
1789 value = strtol(tok, &endptr, 10);
1790 if (endptr == tok || *endptr != '\0') {
1791 ERR(mod->ctx,
1792 "invalid line format at /proc/modules:%d\n", lineno);
1793 break;
1794 }
1795
1796 size = value;
1797 break;
1798 }
1799 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001800
1801done:
1802 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001803 return size;
1804}
1805
1806/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001807 * kmod_module_get_refcnt:
1808 * @mod: kmod module
1809 *
1810 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1811 * /sys filesystem.
1812 *
1813 * Returns: 0 on success or < 0 on failure.
1814 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001815KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1816{
1817 char path[PATH_MAX];
1818 long refcnt;
1819 int fd, err;
1820
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001821 if (mod == NULL)
1822 return -ENOENT;
1823
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001824 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001825 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001826 if (fd < 0) {
1827 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001828 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001829 path, strerror(errno));
1830 return err;
1831 }
1832
1833 err = read_str_long(fd, &refcnt, 10);
1834 close(fd);
1835 if (err < 0) {
1836 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1837 path, strerror(-err));
1838 return err;
1839 }
1840
1841 return (int)refcnt;
1842}
1843
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001844/**
1845 * kmod_module_get_holders:
1846 * @mod: kmod module
1847 *
1848 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1849 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1850 *
1851 * Returns: a new list of kmod modules on success or NULL on failure.
1852 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001853KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1854{
1855 char dname[PATH_MAX];
1856 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001857 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001858 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001859
Lucas De Marchib9a7da32013-04-23 20:47:28 -03001860 if (mod == NULL || mod->ctx == NULL)
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001861 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001862
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001863 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1864
1865 d = opendir(dname);
1866 if (d == NULL) {
1867 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001868 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001869 return NULL;
1870 }
1871
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001872 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001873 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001874 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001875 int err;
1876
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001877 if (dent->d_name[0] == '.') {
1878 if (dent->d_name[1] == '\0' ||
1879 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001880 continue;
1881 }
1882
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001883 err = kmod_module_new_from_name(mod->ctx, dent->d_name,
1884 &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001885 if (err < 0) {
1886 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001887 dent->d_name, strerror(-err));
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001888 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001889 }
1890
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001891 l = kmod_list_append(list, holder);
1892 if (l != NULL) {
1893 list = l;
1894 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001895 ERR(mod->ctx, "out of memory\n");
1896 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001897 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001898 }
1899 }
1900
1901 closedir(d);
1902 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001903
1904fail:
1905 closedir(d);
1906 kmod_module_unref_list(list);
1907 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001908}
1909
1910struct kmod_module_section {
1911 unsigned long address;
1912 char name[];
1913};
1914
1915static void kmod_module_section_free(struct kmod_module_section *section)
1916{
1917 free(section);
1918}
1919
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001920/**
1921 * kmod_module_get_sections:
1922 * @mod: kmod module
1923 *
1924 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1925 * structure contained in this list is internal to libkmod and their fields
1926 * can be obtained by calling kmod_module_section_get_name() and
1927 * kmod_module_section_get_address().
1928 *
1929 * After use, free the @list by calling kmod_module_section_free_list().
1930 *
1931 * Returns: a new list of kmod module sections on success or NULL on failure.
1932 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001933KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1934{
1935 char dname[PATH_MAX];
1936 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001937 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001938 DIR *d;
1939 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001940
1941 if (mod == NULL)
1942 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001943
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001944 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1945
1946 d = opendir(dname);
1947 if (d == NULL) {
1948 ERR(mod->ctx, "could not open '%s': %s\n",
1949 dname, strerror(errno));
1950 return NULL;
1951 }
1952
1953 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001954
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001955 for (dent = readdir(d); dent; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001956 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001957 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001958 unsigned long address;
1959 size_t namesz;
1960 int fd, err;
1961
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001962 if (dent->d_name[0] == '.') {
1963 if (dent->d_name[1] == '\0' ||
1964 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001965 continue;
1966 }
1967
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001968 fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001969 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001970 ERR(mod->ctx, "could not open '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001971 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001972 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001973 }
1974
1975 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001976 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001977 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001978 ERR(mod->ctx, "could not read long from '%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
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001983 namesz = strlen(dent->d_name) + 1;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001984 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001985
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001986 if (section == NULL) {
1987 ERR(mod->ctx, "out of memory\n");
1988 goto fail;
1989 }
1990
1991 section->address = address;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001992 memcpy(section->name, dent->d_name, namesz);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001993
1994 l = kmod_list_append(list, section);
1995 if (l != NULL) {
1996 list = l;
1997 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001998 ERR(mod->ctx, "out of memory\n");
1999 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002000 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002001 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002002 }
2003
2004 closedir(d);
2005 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002006
2007fail:
2008 closedir(d);
2009 kmod_module_unref_list(list);
2010 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002011}
2012
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002013/**
2014 * kmod_module_section_get_module_name:
2015 * @entry: a list entry representing a kmod module section
2016 *
2017 * Get the name of a kmod module section.
2018 *
2019 * After use, free the @list by calling kmod_module_section_free_list().
2020 *
2021 * Returns: the name of this kmod module section on success or NULL on
2022 * failure. The string is owned by the section, do not free it.
2023 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002024KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2025{
2026 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002027
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002028 if (entry == NULL)
2029 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002030
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002031 section = entry->data;
2032 return section->name;
2033}
2034
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002035/**
2036 * kmod_module_section_get_address:
2037 * @entry: a list entry representing a kmod module section
2038 *
2039 * Get the address of a kmod module section.
2040 *
2041 * After use, free the @list by calling kmod_module_section_free_list().
2042 *
2043 * Returns: the address of this kmod module section on success or ULONG_MAX
2044 * on failure.
2045 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002046KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2047{
2048 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002049
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002050 if (entry == NULL)
2051 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002052
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002053 section = entry->data;
2054 return section->address;
2055}
2056
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002057/**
2058 * kmod_module_section_free_list:
2059 * @list: kmod module section list
2060 *
2061 * Release the resources taken by @list
2062 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002063KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2064{
2065 while (list) {
2066 kmod_module_section_free(list->data);
2067 list = kmod_list_remove(list);
2068 }
2069}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002070
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002071static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2072{
2073 if (mod->file == NULL) {
2074 const char *path = kmod_module_get_path(mod);
2075
2076 if (path == NULL) {
2077 errno = ENOENT;
2078 return NULL;
2079 }
2080
2081 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2082 path);
2083 if (mod->file == NULL)
2084 return NULL;
2085 }
2086
2087 return kmod_file_get_elf(mod->file);
2088}
2089
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002090struct kmod_module_info {
2091 char *key;
2092 char value[];
2093};
2094
2095static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2096{
2097 struct kmod_module_info *info;
2098
2099 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2100 if (info == NULL)
2101 return NULL;
2102
2103 info->key = (char *)info + sizeof(struct kmod_module_info)
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002104 + valuelen + 1;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002105 memcpy(info->key, key, keylen);
2106 info->key[keylen] = '\0';
2107 memcpy(info->value, value, valuelen);
2108 info->value[valuelen] = '\0';
2109 return info;
2110}
2111
2112static void kmod_module_info_free(struct kmod_module_info *info)
2113{
2114 free(info);
2115}
2116
Michal Marekf64458c2013-01-16 10:18:17 +01002117static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2118{
2119 struct kmod_module_info *info;
2120 struct kmod_list *n;
2121
2122 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002123 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002124 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002125 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002126 if (n != NULL)
2127 *list = n;
2128 else
Michal Marekf64458c2013-01-16 10:18:17 +01002129 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002130 return n;
2131}
2132
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002133/**
2134 * kmod_module_get_info:
2135 * @mod: kmod module
2136 * @list: where to return list of module information. Use
2137 * kmod_module_info_get_key() and
2138 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002139 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002140 *
2141 * Get a list of entries in ELF section ".modinfo", these contain
2142 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002143 * values. If the module is signed (CONFIG_MODULE_SIG), information
2144 * about the module signature is included as well: signer,
2145 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002146 *
2147 * After use, free the @list by calling kmod_module_info_free_list().
2148 *
2149 * Returns: 0 on success or < 0 otherwise.
2150 */
2151KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2152{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002153 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002154 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002155 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002156 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002157
2158 if (mod == NULL || list == NULL)
2159 return -ENOENT;
2160
2161 assert(*list == NULL);
2162
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002163 elf = kmod_module_get_elf(mod);
2164 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002165 return -errno;
2166
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002167 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002168 if (count < 0)
2169 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002170
2171 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002172 struct kmod_list *n;
2173 const char *key, *value;
2174 size_t keylen, valuelen;
2175
2176 key = strings[i];
2177 value = strchr(key, '=');
2178 if (value == NULL) {
2179 keylen = strlen(key);
2180 valuelen = 0;
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002181 value = key;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002182 } else {
2183 keylen = value - key;
2184 value++;
2185 valuelen = strlen(value);
2186 }
2187
Michal Marekf64458c2013-01-16 10:18:17 +01002188 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2189 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002190 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002191 }
Michal Marek8fe16812013-01-16 09:52:01 +01002192
2193 if (kmod_module_signature_info(mod->file, &sig_info)) {
2194 struct kmod_list *n;
2195 char *key_hex;
2196
2197 n = kmod_module_info_append(list, "signer", strlen("signer"),
2198 sig_info.signer, sig_info.signer_len);
2199 if (n == NULL)
2200 goto list_error;
2201 count++;
2202
2203 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2204 key_hex = malloc(sig_info.key_id_len * 3);
2205 if (key_hex == NULL)
2206 goto list_error;
2207 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2208 sprintf(key_hex + i * 3, "%02X",
2209 (unsigned char)sig_info.key_id[i]);
2210 if (i < (int)sig_info.key_id_len - 1)
2211 key_hex[i * 3 + 2] = ':';
2212 }
2213 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2214 key_hex, sig_info.key_id_len * 3 - 1);
2215 free(key_hex);
2216 if (n == NULL)
2217 goto list_error;
2218 count++;
2219
2220 n = kmod_module_info_append(list,
2221 "sig_hashalgo", strlen("sig_hashalgo"),
2222 sig_info.hash_algo, strlen(sig_info.hash_algo));
2223 if (n == NULL)
2224 goto list_error;
2225 count++;
2226
2227 /*
2228 * Omit sig_info.id_type and sig_info.algo for now, as these
2229 * are currently constant.
2230 */
2231 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002232 ret = count;
2233
2234list_error:
Michal Marek63339342013-01-16 17:51:57 +01002235 if (ret < 0) {
2236 kmod_module_info_free_list(*list);
2237 *list = NULL;
2238 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002239 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002240 return ret;
2241}
2242
2243/**
2244 * kmod_module_info_get_key:
2245 * @entry: a list entry representing a kmod module info
2246 *
2247 * Get the key of a kmod module info.
2248 *
2249 * Returns: the key of this kmod module info on success or NULL on
2250 * failure. The string is owned by the info, do not free it.
2251 */
2252KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2253{
2254 struct kmod_module_info *info;
2255
2256 if (entry == NULL)
2257 return NULL;
2258
2259 info = entry->data;
2260 return info->key;
2261}
2262
2263/**
2264 * kmod_module_info_get_value:
2265 * @entry: a list entry representing a kmod module info
2266 *
2267 * Get the value of a kmod module info.
2268 *
2269 * Returns: the value of this kmod module info on success or NULL on
2270 * failure. The string is owned by the info, do not free it.
2271 */
2272KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2273{
2274 struct kmod_module_info *info;
2275
2276 if (entry == NULL)
2277 return NULL;
2278
2279 info = entry->data;
2280 return info->value;
2281}
2282
2283/**
2284 * kmod_module_info_free_list:
2285 * @list: kmod module info list
2286 *
2287 * Release the resources taken by @list
2288 */
2289KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2290{
2291 while (list) {
2292 kmod_module_info_free(list->data);
2293 list = kmod_list_remove(list);
2294 }
2295}
2296
2297struct kmod_module_version {
2298 uint64_t crc;
2299 char symbol[];
2300};
2301
2302static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2303{
2304 struct kmod_module_version *mv;
2305 size_t symbollen = strlen(symbol) + 1;
2306
2307 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2308 if (mv == NULL)
2309 return NULL;
2310
2311 mv->crc = crc;
2312 memcpy(mv->symbol, symbol, symbollen);
2313 return mv;
2314}
2315
2316static void kmod_module_version_free(struct kmod_module_version *version)
2317{
2318 free(version);
2319}
2320
2321/**
2322 * kmod_module_get_versions:
2323 * @mod: kmod module
2324 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002325 * kmod_module_version_get_symbol() and
2326 * kmod_module_version_get_crc(). Release this list with
2327 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002328 *
2329 * Get a list of entries in ELF section "__versions".
2330 *
2331 * After use, free the @list by calling kmod_module_versions_free_list().
2332 *
2333 * Returns: 0 on success or < 0 otherwise.
2334 */
2335KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2336{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002337 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002338 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002339 int i, count, ret = 0;
2340
2341 if (mod == NULL || list == NULL)
2342 return -ENOENT;
2343
2344 assert(*list == NULL);
2345
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002346 elf = kmod_module_get_elf(mod);
2347 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002348 return -errno;
2349
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002350 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002351 if (count < 0)
2352 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002353
2354 for (i = 0; i < count; i++) {
2355 struct kmod_module_version *mv;
2356 struct kmod_list *n;
2357
2358 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2359 if (mv == NULL) {
2360 ret = -errno;
2361 kmod_module_versions_free_list(*list);
2362 *list = NULL;
2363 goto list_error;
2364 }
2365
2366 n = kmod_list_append(*list, mv);
2367 if (n != NULL)
2368 *list = n;
2369 else {
2370 kmod_module_version_free(mv);
2371 kmod_module_versions_free_list(*list);
2372 *list = NULL;
2373 ret = -ENOMEM;
2374 goto list_error;
2375 }
2376 }
2377 ret = count;
2378
2379list_error:
2380 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002381 return ret;
2382}
2383
2384/**
Chengwei Yang491c4902013-05-04 17:07:02 +08002385 * kmod_module_version_get_symbol:
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002386 * @entry: a list entry representing a kmod module versions
2387 *
2388 * Get the symbol of a kmod module versions.
2389 *
2390 * Returns: the symbol of this kmod module versions on success or NULL
2391 * on failure. The string is owned by the versions, do not free it.
2392 */
2393KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2394{
2395 struct kmod_module_version *version;
2396
2397 if (entry == NULL)
2398 return NULL;
2399
2400 version = entry->data;
2401 return version->symbol;
2402}
2403
2404/**
2405 * kmod_module_version_get_crc:
2406 * @entry: a list entry representing a kmod module version
2407 *
2408 * Get the crc of a kmod module version.
2409 *
2410 * Returns: the crc of this kmod module version on success or NULL on
2411 * failure. The string is owned by the version, do not free it.
2412 */
2413KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2414{
2415 struct kmod_module_version *version;
2416
2417 if (entry == NULL)
2418 return 0;
2419
2420 version = entry->data;
2421 return version->crc;
2422}
2423
2424/**
2425 * kmod_module_versions_free_list:
2426 * @list: kmod module versions list
2427 *
2428 * Release the resources taken by @list
2429 */
2430KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2431{
2432 while (list) {
2433 kmod_module_version_free(list->data);
2434 list = kmod_list_remove(list);
2435 }
2436}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002437
2438struct kmod_module_symbol {
2439 uint64_t crc;
2440 char symbol[];
2441};
2442
2443static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2444{
2445 struct kmod_module_symbol *mv;
2446 size_t symbollen = strlen(symbol) + 1;
2447
2448 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2449 if (mv == NULL)
2450 return NULL;
2451
2452 mv->crc = crc;
2453 memcpy(mv->symbol, symbol, symbollen);
2454 return mv;
2455}
2456
2457static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2458{
2459 free(symbol);
2460}
2461
2462/**
2463 * kmod_module_get_symbols:
2464 * @mod: kmod module
2465 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002466 * kmod_module_symbol_get_symbol() and
2467 * kmod_module_symbol_get_crc(). Release this list with
2468 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002469 *
2470 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2471 *
2472 * After use, free the @list by calling kmod_module_symbols_free_list().
2473 *
2474 * Returns: 0 on success or < 0 otherwise.
2475 */
2476KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2477{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002478 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002479 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002480 int i, count, ret = 0;
2481
2482 if (mod == NULL || list == NULL)
2483 return -ENOENT;
2484
2485 assert(*list == NULL);
2486
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002487 elf = kmod_module_get_elf(mod);
2488 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002489 return -errno;
2490
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002491 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002492 if (count < 0)
2493 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002494
2495 for (i = 0; i < count; i++) {
2496 struct kmod_module_symbol *mv;
2497 struct kmod_list *n;
2498
2499 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2500 if (mv == NULL) {
2501 ret = -errno;
2502 kmod_module_symbols_free_list(*list);
2503 *list = NULL;
2504 goto list_error;
2505 }
2506
2507 n = kmod_list_append(*list, mv);
2508 if (n != NULL)
2509 *list = n;
2510 else {
2511 kmod_module_symbol_free(mv);
2512 kmod_module_symbols_free_list(*list);
2513 *list = NULL;
2514 ret = -ENOMEM;
2515 goto list_error;
2516 }
2517 }
2518 ret = count;
2519
2520list_error:
2521 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002522 return ret;
2523}
2524
2525/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002526 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002527 * @entry: a list entry representing a kmod module symbols
2528 *
2529 * Get the symbol of a kmod module symbols.
2530 *
2531 * Returns: the symbol of this kmod module symbols on success or NULL
2532 * on failure. The string is owned by the symbols, do not free it.
2533 */
2534KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2535{
2536 struct kmod_module_symbol *symbol;
2537
2538 if (entry == NULL)
2539 return NULL;
2540
2541 symbol = entry->data;
2542 return symbol->symbol;
2543}
2544
2545/**
2546 * kmod_module_symbol_get_crc:
2547 * @entry: a list entry representing a kmod module symbol
2548 *
2549 * Get the crc of a kmod module symbol.
2550 *
2551 * Returns: the crc of this kmod module symbol on success or NULL on
2552 * failure. The string is owned by the symbol, do not free it.
2553 */
2554KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2555{
2556 struct kmod_module_symbol *symbol;
2557
2558 if (entry == NULL)
2559 return 0;
2560
2561 symbol = entry->data;
2562 return symbol->crc;
2563}
2564
2565/**
2566 * kmod_module_symbols_free_list:
2567 * @list: kmod module symbols list
2568 *
2569 * Release the resources taken by @list
2570 */
2571KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2572{
2573 while (list) {
2574 kmod_module_symbol_free(list->data);
2575 list = kmod_list_remove(list);
2576 }
2577}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002578
2579struct kmod_module_dependency_symbol {
2580 uint64_t crc;
2581 uint8_t bind;
2582 char symbol[];
2583};
2584
2585static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2586{
2587 struct kmod_module_dependency_symbol *mv;
2588 size_t symbollen = strlen(symbol) + 1;
2589
2590 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2591 if (mv == NULL)
2592 return NULL;
2593
2594 mv->crc = crc;
2595 mv->bind = bind;
2596 memcpy(mv->symbol, symbol, symbollen);
2597 return mv;
2598}
2599
2600static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2601{
2602 free(dependency_symbol);
2603}
2604
2605/**
2606 * kmod_module_get_dependency_symbols:
2607 * @mod: kmod module
2608 * @list: where to return list of module dependency_symbols. Use
2609 * kmod_module_dependency_symbol_get_symbol() and
2610 * kmod_module_dependency_symbol_get_crc(). Release this list with
2611 * kmod_module_dependency_symbols_free_list()
2612 *
2613 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2614 *
2615 * After use, free the @list by calling
2616 * kmod_module_dependency_symbols_free_list().
2617 *
2618 * Returns: 0 on success or < 0 otherwise.
2619 */
2620KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2621{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002622 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002623 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002624 int i, count, ret = 0;
2625
2626 if (mod == NULL || list == NULL)
2627 return -ENOENT;
2628
2629 assert(*list == NULL);
2630
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002631 elf = kmod_module_get_elf(mod);
2632 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002633 return -errno;
2634
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002635 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002636 if (count < 0)
2637 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002638
2639 for (i = 0; i < count; i++) {
2640 struct kmod_module_dependency_symbol *mv;
2641 struct kmod_list *n;
2642
2643 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2644 symbols[i].bind,
2645 symbols[i].symbol);
2646 if (mv == NULL) {
2647 ret = -errno;
2648 kmod_module_dependency_symbols_free_list(*list);
2649 *list = NULL;
2650 goto list_error;
2651 }
2652
2653 n = kmod_list_append(*list, mv);
2654 if (n != NULL)
2655 *list = n;
2656 else {
2657 kmod_module_dependency_symbol_free(mv);
2658 kmod_module_dependency_symbols_free_list(*list);
2659 *list = NULL;
2660 ret = -ENOMEM;
2661 goto list_error;
2662 }
2663 }
2664 ret = count;
2665
2666list_error:
2667 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002668 return ret;
2669}
2670
2671/**
2672 * kmod_module_dependency_symbol_get_symbol:
2673 * @entry: a list entry representing a kmod module dependency_symbols
2674 *
2675 * Get the dependency symbol of a kmod module
2676 *
2677 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2678 * on failure. The string is owned by the dependency_symbols, do not free it.
2679 */
2680KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2681{
2682 struct kmod_module_dependency_symbol *dependency_symbol;
2683
2684 if (entry == NULL)
2685 return NULL;
2686
2687 dependency_symbol = entry->data;
2688 return dependency_symbol->symbol;
2689}
2690
2691/**
2692 * kmod_module_dependency_symbol_get_crc:
2693 * @entry: a list entry representing a kmod module dependency_symbol
2694 *
2695 * Get the crc of a kmod module dependency_symbol.
2696 *
2697 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2698 * failure. The string is owned by the dependency_symbol, do not free it.
2699 */
2700KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2701{
2702 struct kmod_module_dependency_symbol *dependency_symbol;
2703
2704 if (entry == NULL)
2705 return 0;
2706
2707 dependency_symbol = entry->data;
2708 return dependency_symbol->crc;
2709}
2710
2711/**
2712 * kmod_module_dependency_symbol_get_bind:
2713 * @entry: a list entry representing a kmod module dependency_symbol
2714 *
2715 * Get the bind type of a kmod module dependency_symbol.
2716 *
2717 * Returns: the bind of this kmod module dependency_symbol on success
2718 * or < 0 on failure.
2719 */
2720KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2721{
2722 struct kmod_module_dependency_symbol *dependency_symbol;
2723
2724 if (entry == NULL)
2725 return 0;
2726
2727 dependency_symbol = entry->data;
2728 return dependency_symbol->bind;
2729}
2730
2731/**
2732 * kmod_module_dependency_symbols_free_list:
2733 * @list: kmod module dependency_symbols list
2734 *
2735 * Release the resources taken by @list
2736 */
2737KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2738{
2739 while (list) {
2740 kmod_module_dependency_symbol_free(list->data);
2741 list = kmod_list_remove(list);
2742 }
2743}