blob: 57da0a2d11479d8e1ee85afd10e95f38f0817a19 [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
Lucas De Marchidea2dfe2014-12-25 23:32:03 -020017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchi8f788d52011-11-25 01:22:56 -020018 */
19
Lucas De Marchi7636e722011-12-01 17:56:03 -020020#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020021#include <ctype.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030022#include <dirent.h>
23#include <errno.h>
24#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020025#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020026#include <limits.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030027#include <stdarg.h>
28#include <stddef.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/mman.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030034#include <sys/stat.h>
Kees Cook144d1822013-02-18 12:02:32 -080035#include <sys/syscall.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030036#include <sys/types.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020037#include <sys/wait.h>
Kees Cook144d1822013-02-18 12:02:32 -080038#ifdef HAVE_LINUX_MODULE_H
39#include <linux/module.h>
40#endif
41
Lucas De Marchi96573a02014-10-03 00:01:35 -030042#include <shared/util.h>
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
Harish Jenny K Nfd44a982015-02-22 15:41:07 -030052enum kmod_module_builtin {
53 KMOD_MODULE_BUILTIN_UNKNOWN,
54 KMOD_MODULE_BUILTIN_NO,
55 KMOD_MODULE_BUILTIN_YES,
56};
57
Lucas De Marchi66819512012-01-09 04:47:40 -020058/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020059 * kmod_module:
60 *
61 * Opaque object representing a module.
62 */
63struct kmod_module {
64 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020065 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020066 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020067 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020068 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020069 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020070 const char *install_commands; /* owned by kmod_config */
71 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020072 char *alias; /* only set if this module was created from an alias */
Lucas De Marchi1eff9422012-10-18 01:36:33 -030073 struct kmod_file *file;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020074 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020075 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020076 struct {
77 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020078 bool options : 1;
79 bool install_commands : 1;
80 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020081 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020082
83 /*
Harish Jenny K Nfd44a982015-02-22 15:41:07 -030084 * mark if module is builtin, i.e. it's present on modules.builtin
85 * file. This is set as soon as it is needed or as soon as we know
86 * about it, i.e. the module was created from builtin lookup.
87 */
88 enum kmod_module_builtin builtin;
89
90 /*
Lucas De Marchib1a51252012-01-29 01:49:09 -020091 * private field used by kmod_module_get_probe_list() to detect
92 * dependency loops
93 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020094 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020095
96 /*
97 * set by kmod_module_get_probe_list: indicates for probe_insert()
98 * whether the module's command and softdep should be ignored
99 */
100 bool ignorecmd : 1;
Lucas De Marchi38052742012-02-16 20:43:16 -0200101
102 /*
Michal Marek450bd1b2014-03-31 15:18:50 +0200103 * set by kmod_module_get_probe_list: indicates whether this is the
104 * module the user asked for or its dependency, or whether this
105 * is a softdep only
106 */
107 bool required : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200108};
109
Lucas De Marchic35347f2011-12-12 10:48:02 -0200110static inline const char *path_join(const char *path, size_t prefixlen,
111 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200112{
113 size_t pathlen;
114
115 if (path[0] == '/')
116 return path;
117
118 pathlen = strlen(path);
119 if (prefixlen + pathlen + 1 >= PATH_MAX)
120 return NULL;
121
122 memcpy(buf + prefixlen, path, pathlen + 1);
123 return buf;
124}
125
Dave Reisneraf9572c2012-02-02 11:07:33 -0500126static inline bool module_is_inkernel(struct kmod_module *mod)
127{
128 int state = kmod_module_get_initstate(mod);
Lucas De Marchi38052742012-02-16 20:43:16 -0200129
Dave Reisneraf9572c2012-02-02 11:07:33 -0500130 if (state == KMOD_MODULE_LIVE ||
Dave Reisneraf9572c2012-02-02 11:07:33 -0500131 state == KMOD_MODULE_BUILTIN)
132 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200133
134 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500135}
136
Lucas De Marchi671d4892011-12-05 20:23:05 -0200137int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200138{
139 struct kmod_ctx *ctx = mod->ctx;
140 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200141 const char *dirname;
142 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200143 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200144 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200145 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200146
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200147 if (mod->init.dep)
148 return mod->n_dep;
149 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200150 mod->init.dep = true;
151
152 p = strchr(line, ':');
153 if (p == NULL)
154 return 0;
155
Lucas De Marchi671d4892011-12-05 20:23:05 -0200156 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200157 dirname = kmod_get_dirname(mod->ctx);
158 dirnamelen = strlen(dirname);
159 if (dirnamelen + 2 >= PATH_MAX)
160 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200161
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200162 memcpy(buf, dirname, dirnamelen);
163 buf[dirnamelen] = '/';
164 dirnamelen++;
165 buf[dirnamelen] = '\0';
166
167 if (mod->path == NULL) {
168 const char *str = path_join(line, dirnamelen, buf);
169 if (str == NULL)
170 return 0;
171 mod->path = strdup(str);
172 if (mod->path == NULL)
173 return 0;
174 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200175
Lucas De Marchi7636e722011-12-01 17:56:03 -0200176 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200177 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
178 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi01f9bc62015-01-25 23:54:05 -0200179 struct kmod_module *depmod = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200180 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200181
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200182 path = path_join(p, dirnamelen, buf);
183 if (path == NULL) {
184 ERR(ctx, "could not join path '%s' and '%s'.\n",
185 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200186 goto fail;
187 }
188
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200189 err = kmod_module_new_from_path(ctx, path, &depmod);
190 if (err < 0) {
191 ERR(ctx, "ctx=%p path=%s error=%s\n",
192 ctx, path, strerror(-err));
193 goto fail;
194 }
195
196 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200197
Lucas De Marchib94a7372011-12-26 20:10:49 -0200198 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200199 n++;
200 }
201
202 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
203
204 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200205 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200206 return n;
207
208fail:
209 kmod_module_unref_list(list);
210 mod->init.dep = false;
211 return err;
212}
213
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200214void kmod_module_set_visited(struct kmod_module *mod, bool visited)
215{
216 mod->visited = visited;
217}
218
Lucas De Marchi38052742012-02-16 20:43:16 -0200219void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
220{
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300221 mod->builtin =
222 builtin ? KMOD_MODULE_BUILTIN_YES : KMOD_MODULE_BUILTIN_NO;
Lucas De Marchi38052742012-02-16 20:43:16 -0200223}
224
Michal Marek450bd1b2014-03-31 15:18:50 +0200225void kmod_module_set_required(struct kmod_module *mod, bool required)
226{
227 mod->required = required;
228}
229
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300230bool kmod_module_is_builtin(struct kmod_module *mod)
231{
232 if (mod->builtin == KMOD_MODULE_BUILTIN_UNKNOWN) {
233 kmod_module_set_builtin(mod,
234 kmod_lookup_alias_is_builtin(mod->ctx, mod->name));
235 }
236
237 return mod->builtin == KMOD_MODULE_BUILTIN_YES;
238}
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200239/*
240 * Memory layout with alias:
241 *
242 * struct kmod_module {
243 * hashkey -----.
244 * alias -----. |
245 * name ----. | |
246 * } | | |
247 * name <----------' | |
248 * alias <-----------' |
249 * name\alias <--------'
250 *
251 * Memory layout without alias:
252 *
253 * struct kmod_module {
254 * hashkey ---.
255 * alias -----|----> NULL
256 * name ----. |
257 * } | |
258 * name <----------'-'
259 *
260 * @key is "name\alias" or "name" (in which case alias == NULL)
261 */
262static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
263 const char *name, size_t namelen,
264 const char *alias, size_t aliaslen,
265 struct kmod_module **mod)
266{
267 struct kmod_module *m;
268 size_t keylen;
269
270 m = kmod_pool_get_module(ctx, key);
271 if (m != NULL) {
272 *mod = kmod_module_ref(m);
273 return 0;
274 }
275
276 if (alias == NULL)
277 keylen = namelen;
278 else
279 keylen = namelen + aliaslen + 1;
280
281 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
Lucas De Marchi9f025612013-11-18 11:52:53 -0200282 if (m == NULL)
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200283 return -ENOMEM;
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200284
285 memset(m, 0, sizeof(*m));
286
287 m->ctx = kmod_ref(ctx);
288 m->name = (char *)m + sizeof(*m);
289 memcpy(m->name, key, keylen + 1);
290 if (alias == NULL) {
291 m->hashkey = m->name;
292 m->alias = NULL;
293 } else {
294 m->name[namelen] = '\0';
295 m->alias = m->name + namelen + 1;
296 m->hashkey = m->name + keylen + 1;
297 memcpy(m->hashkey, key, keylen + 1);
298 }
299
300 m->refcount = 1;
301 kmod_pool_add_module(ctx, m, m->hashkey);
302 *mod = m;
303
304 return 0;
305}
306
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200307/**
308 * kmod_module_new_from_name:
309 * @ctx: kmod library context
310 * @name: name of the module
311 * @mod: where to save the created struct kmod_module
312 *
313 * Create a new struct kmod_module using the module name. @name can not be an
314 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600315 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200316 *
317 * This function is also used internally by many others that return a new
318 * struct kmod_module or a new list of modules.
319 *
320 * The initial refcount is 1, and needs to be decremented to release the
321 * resources of the kmod_module. Since libkmod keeps track of all
322 * kmod_modules created, they are all released upon @ctx destruction too. Do
323 * not unref @ctx before all the desired operations with the returned
324 * kmod_module are done.
325 *
326 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
327 * module name or if memory allocation failed.
328 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200329KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
330 const char *name,
331 struct kmod_module **mod)
332{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200333 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200334 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200335
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200336 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200337 return -ENOENT;
338
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200339 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200340
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200341 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200342}
343
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200344int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
345 const char *name, struct kmod_module **mod)
346{
347 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200348 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200349 size_t namelen = strlen(name);
350 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200351
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200352 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200353 return -ENAMETOOLONG;
354
355 memcpy(key, name, namelen);
356 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200357 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200358
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200359 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200360 if (err < 0)
361 return err;
362
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200363 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200364}
365
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200366/**
367 * kmod_module_new_from_path:
368 * @ctx: kmod library context
369 * @path: path where to find the given module
370 * @mod: where to save the created struct kmod_module
371 *
372 * Create a new struct kmod_module using the module path. @path must be an
373 * existent file with in the filesystem and must be accessible to libkmod.
374 *
375 * The initial refcount is 1, and needs to be decremented to release the
376 * resources of the kmod_module. Since libkmod keeps track of all
377 * kmod_modules created, they are all released upon @ctx destruction too. Do
378 * not unref @ctx before all the desired operations with the returned
379 * kmod_module are done.
380 *
381 * If @path is relative, it's treated as relative to the current working
382 * directory. Otherwise, give an absolute path.
383 *
384 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
385 * it's not a valid file for a kmod_module or if memory allocation failed.
386 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200387KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
388 const char *path,
389 struct kmod_module **mod)
390{
391 struct kmod_module *m;
392 int err;
393 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200394 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200395 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200396 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200397
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200398 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200399 return -ENOENT;
400
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200401 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200402 if (abspath == NULL) {
403 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200404 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200405 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200406
407 err = stat(abspath, &st);
408 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200409 err = -errno;
410 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200411 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200412 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200413 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200414
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200415 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200416 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200417 free(abspath);
418 return -ENOENT;
419 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200420
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200421 m = kmod_pool_get_module(ctx, name);
422 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200423 if (m->path == NULL)
424 m->path = abspath;
425 else if (streq(m->path, abspath))
426 free(abspath);
427 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200428 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 -0200429 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200430 free(abspath);
431 return -EEXIST;
432 }
433
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200434 *mod = kmod_module_ref(m);
435 return 0;
436 }
437
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200438 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
Leandro Pereirac1bc88c2014-04-28 21:02:45 -0300439 if (err < 0) {
440 free(abspath);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200441 return err;
Leandro Pereirac1bc88c2014-04-28 21:02:45 -0300442 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200443
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200444 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200445 *mod = m;
446
447 return 0;
448}
449
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200450/**
451 * kmod_module_unref:
452 * @mod: kmod module
453 *
454 * Drop a reference of the kmod module. If the refcount reaches zero, its
455 * resources are released.
456 *
457 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
458 * returns the passed @mod with its refcount decremented.
459 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200460KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
461{
462 if (mod == NULL)
463 return NULL;
464
465 if (--mod->refcount > 0)
466 return mod;
467
468 DBG(mod->ctx, "kmod_module %p released\n", mod);
469
Lucas De Marchi4084c172011-12-15 13:43:22 -0200470 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200471 kmod_module_unref_list(mod->dep);
Lucas De Marchi1eff9422012-10-18 01:36:33 -0300472
473 if (mod->file)
474 kmod_file_unref(mod->file);
475
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200476 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200477 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200478 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200479 free(mod);
480 return NULL;
481}
482
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200483/**
484 * kmod_module_ref:
485 * @mod: kmod module
486 *
487 * Take a reference of the kmod module, incrementing its refcount.
488 *
489 * Returns: the passed @module with its refcount incremented.
490 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200491KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
492{
493 if (mod == NULL)
494 return NULL;
495
496 mod->refcount++;
497
498 return mod;
499}
500
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200501#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
502 do { \
503 if ((_err) < 0) \
504 goto _label_err; \
505 if (*(_list) != NULL) \
506 goto finish; \
507 } while (0)
508
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200509/**
510 * kmod_module_new_from_lookup:
511 * @ctx: kmod library context
512 * @given_alias: alias to look for
513 * @list: an empty list where to save the list of modules matching
514 * @given_alias
515 *
516 * Create a new list of kmod modules using an alias or module name and lookup
517 * libkmod's configuration files and indexes in order to find the module.
518 * Once it's found in one of the places, it stops searching and create the
519 * list of modules that is saved in @list.
520 *
521 * The search order is: 1. aliases in configuration file; 2. module names in
522 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
523 * in modules.alias index.
524 *
525 * The initial refcount is 1, and needs to be decremented to release the
526 * resources of the kmod_module. The returned @list must be released by
527 * calling kmod_module_unref_list(). Since libkmod keeps track of all
528 * kmod_modules created, they are all released upon @ctx destruction too. Do
529 * not unref @ctx before all the desired operations with the returned list are
530 * completed.
531 *
532 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
533 * methods failed, which is basically due to memory allocation fail. If module
534 * is not found, it still returns 0, but @list is an empty list.
535 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200536KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200537 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200538 struct kmod_list **list)
539{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200540 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200541 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200542
Lucas De Marchi4308b172011-12-13 10:26:04 -0200543 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200544 return -ENOENT;
545
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200546 if (list == NULL || *list != NULL) {
547 ERR(ctx, "An empty list is needed to create lookup\n");
548 return -ENOSYS;
549 }
550
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200551 if (alias_normalize(given_alias, alias, NULL) < 0) {
552 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200553 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200554 }
555
556 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200557
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200558 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200559 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200560 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200561
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200562 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200563 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
564 CHECK_ERR_AND_FINISH(err, fail, list, finish);
565
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200566 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200567 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
568 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200569
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200570 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200571 err = kmod_lookup_alias_from_commands(ctx, alias, list);
572 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200573
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200574 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200575 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
576 CHECK_ERR_AND_FINISH(err, fail, list, finish);
577
Lucas De Marchi38052742012-02-16 20:43:16 -0200578 DBG(ctx, "lookup modules.builtin %s\n", alias);
579 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
580 CHECK_ERR_AND_FINISH(err, fail, list, finish);
581
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200582finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200583 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200584 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200585fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200586 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200587 kmod_module_unref_list(*list);
588 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200589 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200590}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200591#undef CHECK_ERR_AND_FINISH
592
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200593/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200594 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200595 * @list: list of kmod modules
596 *
597 * Drop a reference of each kmod module in @list and releases the resources
598 * taken by the list itself.
599 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800600 * Returns: 0
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200601 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200602KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
603{
604 for (; list != NULL; list = kmod_list_remove(list))
605 kmod_module_unref(list->data);
606
607 return 0;
608}
609
Lucas De Marchi0d467432011-12-31 11:15:52 -0200610/**
611 * kmod_module_get_filtered_blacklist:
612 * @ctx: kmod library context
613 * @input: list of kmod_module to be filtered with blacklist
614 * @output: where to save the new list
615 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200616 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500617 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200618 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500619 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200620 *
621 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
622 * list.
623 */
624KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
625 const struct kmod_list *input,
626 struct kmod_list **output)
627{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500628 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200629}
630
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200631static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
632{
633 if (!mod->init.dep) {
634 /* lazy init */
635 char *line = kmod_search_moddep(mod->ctx, mod->name);
636
637 if (line == NULL)
638 return NULL;
639
640 kmod_module_parse_depline((struct kmod_module *)mod, line);
641 free(line);
642
643 if (!mod->init.dep)
644 return NULL;
645 }
646
647 return mod->dep;
648}
649
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200650/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200651 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200652 * @mod: kmod module
653 *
654 * Search the modules.dep index to find the dependencies of the given @mod.
655 * The result is cached in @mod, so subsequent calls to this function will
656 * return the already searched list of modules.
657 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800658 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
659 * that can be released by calling kmod_module_unref_list().
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200660 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200661KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200662{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200663 struct kmod_list *l, *l_new, *list_new = NULL;
664
665 if (mod == NULL)
666 return NULL;
667
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200668 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200669
670 kmod_list_foreach(l, mod->dep) {
671 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
672 if (l_new == NULL) {
673 kmod_module_unref(l->data);
674 goto fail;
675 }
676
677 list_new = l_new;
678 }
679
680 return list_new;
681
682fail:
683 ERR(mod->ctx, "out of memory\n");
684 kmod_module_unref_list(list_new);
685 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200686}
687
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200688/**
689 * kmod_module_get_module:
690 * @entry: an entry in a list of kmod modules.
691 *
692 * Get the kmod module of this @entry in the list, increasing its refcount.
693 * After it's used, unref it. Since the refcount is incremented upon return,
694 * you still have to call kmod_module_unref_list() to release the list of kmod
695 * modules.
696 *
697 * Returns: NULL on failure or the kmod_module contained in this list entry
698 * with its refcount incremented.
699 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200700KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200701{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200702 if (entry == NULL)
703 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200704
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200705 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200706}
707
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200708/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200709 * kmod_module_get_name:
710 * @mod: kmod module
711 *
712 * Get the name of this kmod module. Name is always available, independently
713 * if it was created by kmod_module_new_from_name() or another function and
714 * it's always normalized (dashes are replaced with underscores).
715 *
716 * Returns: the name of this kmod module.
717 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200718KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200719{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200720 if (mod == NULL)
721 return NULL;
722
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200723 return mod->name;
724}
725
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200726/**
727 * kmod_module_get_path:
728 * @mod: kmod module
729 *
730 * Get the path of this kmod module. If this kmod module was not created by
731 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200732 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200733 *
734 * Returns: the path of this kmod module or NULL if such information is not
735 * available.
736 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200737KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200738{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200739 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200740
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200741 if (mod == NULL)
742 return NULL;
743
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200744 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200745
Lucas De Marchie005fac2011-12-08 10:42:34 -0200746 if (mod->path != NULL)
747 return mod->path;
748 if (mod->init.dep)
749 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200750
Lucas De Marchie005fac2011-12-08 10:42:34 -0200751 /* lazy init */
752 line = kmod_search_moddep(mod->ctx, mod->name);
753 if (line == NULL)
754 return NULL;
755
756 kmod_module_parse_depline((struct kmod_module *) mod, line);
757 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200758
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200759 return mod->path;
760}
761
762
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200763extern long delete_module(const char *name, unsigned int flags);
764
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200765/**
766 * kmod_module_remove_module:
767 * @mod: kmod module
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500768 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
Chengwei Yangd7152f62013-05-04 17:07:03 +0800769 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
770 * use by a kernel subsystem or other process;
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500771 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
772 * delete_module(2).
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200773 *
774 * Remove a module from Linux kernel.
775 *
776 * Returns: 0 on success or < 0 on failure.
777 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200778KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
779 unsigned int flags)
780{
781 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200782
783 if (mod == NULL)
784 return -ENOENT;
785
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500786 /* Filter out other flags and force ONONBLOCK */
787 flags &= KMOD_REMOVE_FORCE;
788 flags |= KMOD_REMOVE_NOWAIT;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200789
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200790 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200791 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200792 err = -errno;
793 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200794 }
795
Lucas De Marchiba998b92012-01-11 00:08:14 -0200796 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200797}
798
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200799extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200800
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200801/**
802 * kmod_module_insert_module:
803 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200804 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +0800805 * behavior of this function, valid flags are
806 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
807 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200808 * @options: module's options to pass to Linux Kernel.
809 *
810 * Insert a module in Linux kernel. It opens the file pointed by @mod,
811 * mmap'ing it and passing to kernel.
812 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200813 * Returns: 0 on success or < 0 on failure. If module is already loaded it
814 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200815 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200816KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200817 unsigned int flags,
818 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200819{
820 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200821 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200822 off_t size;
Michal Marekc2f4d852014-02-28 13:05:32 +0100823 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200824 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200825 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200826
827 if (mod == NULL)
828 return -ENOENT;
829
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200830 path = kmod_module_get_path(mod);
831 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500832 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi114ec872015-06-13 18:29:47 -0300833 return -ENOENT;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200834 }
835
Bartosz Golaszewskib1982672017-02-15 12:18:03 +0100836 if (!mod->file) {
837 mod->file = kmod_file_open(mod->ctx, path);
838 if (mod->file == NULL) {
839 err = -errno;
840 return err;
841 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200842 }
843
Michal Marekc2f4d852014-02-28 13:05:32 +0100844 if (kmod_file_get_direct(mod->file)) {
Kees Cook144d1822013-02-18 12:02:32 -0800845 unsigned int kernel_flags = 0;
846
Kees Cook144d1822013-02-18 12:02:32 -0800847 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
848 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
849 if (flags & KMOD_INSERT_FORCE_MODVERSION)
850 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
Kees Cook144d1822013-02-18 12:02:32 -0800851
Michal Marekc2f4d852014-02-28 13:05:32 +0100852 err = finit_module(kmod_file_get_fd(mod->file), args, kernel_flags);
Kees Cook144d1822013-02-18 12:02:32 -0800853 if (err == 0 || errno != ENOSYS)
854 goto init_finished;
855 }
856
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200857 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
Michal Marekc2f4d852014-02-28 13:05:32 +0100858 elf = kmod_file_get_elf(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200859 if (elf == NULL) {
860 err = -errno;
Michal Marekc2f4d852014-02-28 13:05:32 +0100861 return err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200862 }
863
864 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
865 err = kmod_elf_strip_section(elf, "__versions");
866 if (err < 0)
867 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
868 }
869
870 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
871 err = kmod_elf_strip_vermagic(elf);
872 if (err < 0)
873 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
874 }
875
876 mem = kmod_elf_get_memory(elf);
Michal Marekc2f4d852014-02-28 13:05:32 +0100877 } else {
878 mem = kmod_file_get_contents(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200879 }
Michal Marekc2f4d852014-02-28 13:05:32 +0100880 size = kmod_file_get_size(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200881
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200882 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800883init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200884 if (err < 0) {
885 err = -errno;
886 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
887 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200888 return err;
889}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200890
Lucas De Marchiddbda022011-12-27 11:40:10 -0200891static bool module_is_blacklisted(struct kmod_module *mod)
892{
893 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300894 const struct kmod_config *config = kmod_get_config(ctx);
895 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200896 const struct kmod_list *l;
897
898 kmod_list_foreach(l, bl) {
899 const char *modname = kmod_blacklist_get_modname(l);
900
901 if (streq(modname, mod->name))
902 return true;
903 }
904
905 return false;
906}
907
Dave Reisnerd80b1032012-02-24 10:05:11 -0500908/**
909 * kmod_module_apply_filter
910 * @ctx: kmod library context
Chengwei Yangd7152f62013-05-04 17:07:03 +0800911 * @filter_type: bitmask to filter modules out, valid types are
912 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
913 * KMOD_FILTER_BUILTIN: filter builtin modules out.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500914 * @input: list of kmod_module to be filtered
915 * @output: where to save the new list
916 *
917 * Given a list @input, this function filter it out by the filter mask
918 * and save it in @output.
919 *
920 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
921 * list.
922 */
923KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
924 enum kmod_filter filter_type,
925 const struct kmod_list *input,
926 struct kmod_list **output)
927{
928 const struct kmod_list *li;
929
930 if (ctx == NULL || output == NULL)
931 return -ENOENT;
932
933 *output = NULL;
934 if (input == NULL)
935 return 0;
936
937 kmod_list_foreach(li, input) {
938 struct kmod_module *mod = li->data;
939 struct kmod_list *node;
940
941 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
942 module_is_blacklisted(mod))
943 continue;
944
Harish Jenny K Nfd44a982015-02-22 15:41:07 -0300945 if ((filter_type & KMOD_FILTER_BUILTIN)
946 && kmod_module_is_builtin(mod))
Dave Reisnerd80b1032012-02-24 10:05:11 -0500947 continue;
948
949 node = kmod_list_append(*output, mod);
950 if (node == NULL)
951 goto fail;
952
953 *output = node;
954 kmod_module_ref(mod);
955 }
956
957 return 0;
958
959fail:
960 kmod_module_unref_list(*output);
961 *output = NULL;
962 return -ENOMEM;
963}
964
Lucas De Marchiddbda022011-12-27 11:40:10 -0200965static int command_do(struct kmod_module *mod, const char *type,
966 const char *cmd)
967{
968 const char *modname = kmod_module_get_name(mod);
969 int err;
970
971 DBG(mod->ctx, "%s %s\n", type, cmd);
972
973 setenv("MODPROBE_MODULE", modname, 1);
974 err = system(cmd);
975 unsetenv("MODPROBE_MODULE");
976
977 if (err == -1 || WEXITSTATUS(err)) {
978 ERR(mod->ctx, "Error running %s command for %s\n",
979 type, modname);
980 if (err != -1)
981 err = -WEXITSTATUS(err);
982 }
983
984 return err;
985}
986
Lucas De Marchib1a51252012-01-29 01:49:09 -0200987struct probe_insert_cb {
988 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
989 void *data;
990};
991
Lucas De Marchiddbda022011-12-27 11:40:10 -0200992static int module_do_install_commands(struct kmod_module *mod,
993 const char *options,
994 struct probe_insert_cb *cb)
995{
996 const char *command = kmod_module_get_install_commands(mod);
Lucas De Marchi9f025612013-11-18 11:52:53 -0200997 char *p;
998 _cleanup_free_ char *cmd;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200999 int err;
1000 size_t cmdlen, options_len, varlen;
1001
1002 assert(command);
1003
1004 if (options == NULL)
1005 options = "";
1006
1007 options_len = strlen(options);
1008 cmdlen = strlen(command);
1009 varlen = sizeof("$CMDLINE_OPTS") - 1;
1010
1011 cmd = memdup(command, cmdlen + 1);
1012 if (cmd == NULL)
1013 return -ENOMEM;
1014
1015 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
1016 size_t prefixlen = p - cmd;
1017 size_t suffixlen = cmdlen - prefixlen - varlen;
1018 size_t slen = cmdlen - varlen + options_len;
1019 char *suffix = p + varlen;
1020 char *s = malloc(slen + 1);
Lucas De Marchi9f025612013-11-18 11:52:53 -02001021 if (!s)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001022 return -ENOMEM;
Lucas De Marchi9f025612013-11-18 11:52:53 -02001023
Lucas De Marchiddbda022011-12-27 11:40:10 -02001024 memcpy(s, cmd, p - cmd);
1025 memcpy(s + prefixlen, options, options_len);
1026 memcpy(s + prefixlen + options_len, suffix, suffixlen);
1027 s[slen] = '\0';
1028
1029 free(cmd);
1030 cmd = s;
1031 cmdlen = slen;
1032 }
1033
1034 if (cb->run_install != NULL)
1035 err = cb->run_install(mod, cmd, cb->data);
1036 else
1037 err = command_do(mod, "install", cmd);
1038
Lucas De Marchiddbda022011-12-27 11:40:10 -02001039 return err;
1040}
1041
Lucas De Marchiddbda022011-12-27 11:40:10 -02001042static char *module_options_concat(const char *opt, const char *xopt)
1043{
1044 // TODO: we might need to check if xopt overrides options on opt
1045 size_t optlen = opt == NULL ? 0 : strlen(opt);
1046 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1047 char *r;
1048
1049 if (optlen == 0 && xoptlen == 0)
1050 return NULL;
1051
1052 r = malloc(optlen + xoptlen + 2);
1053
1054 if (opt != NULL) {
1055 memcpy(r, opt, optlen);
1056 r[optlen] = ' ';
1057 optlen++;
1058 }
1059
1060 if (xopt != NULL)
1061 memcpy(r + optlen, xopt, xoptlen);
1062
1063 r[optlen + xoptlen] = '\0';
1064
1065 return r;
1066}
1067
Lucas De Marchib1a51252012-01-29 01:49:09 -02001068static int __kmod_module_get_probe_list(struct kmod_module *mod,
Michal Marek450bd1b2014-03-31 15:18:50 +02001069 bool required,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001070 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001071 struct kmod_list **list);
1072
1073/* re-entrant */
1074static int __kmod_module_fill_softdep(struct kmod_module *mod,
1075 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001076{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001077 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001078 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001079
1080 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001081 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001082 ERR(mod->ctx, "could not get softdep: %s\n",
1083 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001084 goto fail;
1085 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001086
Lucas De Marchib1a51252012-01-29 01:49:09 -02001087 kmod_list_foreach(l, pre) {
1088 struct kmod_module *m = l->data;
Michal Marek450bd1b2014-03-31 15:18:50 +02001089 err = __kmod_module_get_probe_list(m, false, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001090 if (err < 0)
1091 goto fail;
1092 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001093
Lucas De Marchib1a51252012-01-29 01:49:09 -02001094 l = kmod_list_append(*list, kmod_module_ref(mod));
1095 if (l == NULL) {
1096 kmod_module_unref(mod);
1097 err = -ENOMEM;
1098 goto fail;
1099 }
1100 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001101 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001102
Lucas De Marchib1a51252012-01-29 01:49:09 -02001103 kmod_list_foreach(l, post) {
1104 struct kmod_module *m = l->data;
Michal Marek450bd1b2014-03-31 15:18:50 +02001105 err = __kmod_module_get_probe_list(m, false, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001106 if (err < 0)
1107 goto fail;
1108 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001109
Lucas De Marchib1a51252012-01-29 01:49:09 -02001110fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001111 kmod_module_unref_list(pre);
1112 kmod_module_unref_list(post);
1113
1114 return err;
1115}
1116
Lucas De Marchib1a51252012-01-29 01:49:09 -02001117/* re-entrant */
1118static int __kmod_module_get_probe_list(struct kmod_module *mod,
Michal Marek450bd1b2014-03-31 15:18:50 +02001119 bool required,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001120 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001121 struct kmod_list **list)
1122{
1123 struct kmod_list *dep, *l;
1124 int err = 0;
1125
1126 if (mod->visited) {
1127 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1128 mod->name);
1129 return 0;
1130 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001131 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001132
1133 dep = kmod_module_get_dependencies(mod);
Michal Marek450bd1b2014-03-31 15:18:50 +02001134 if (required) {
1135 /*
1136 * Called from kmod_module_probe_insert_module(); set the
1137 * ->required flag on mod and all its dependencies before
1138 * they are possibly visited through some softdeps.
1139 */
1140 mod->required = true;
1141 kmod_list_foreach(l, dep) {
1142 struct kmod_module *m = l->data;
1143 m->required = true;
1144 }
1145 }
1146
Lucas De Marchib1a51252012-01-29 01:49:09 -02001147 kmod_list_foreach(l, dep) {
1148 struct kmod_module *m = l->data;
1149 err = __kmod_module_fill_softdep(m, list);
1150 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001151 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001152 }
1153
Lucas De Marchi89e92482012-01-29 02:35:46 -02001154 if (ignorecmd) {
1155 l = kmod_list_append(*list, kmod_module_ref(mod));
1156 if (l == NULL) {
1157 kmod_module_unref(mod);
1158 err = -ENOMEM;
1159 goto finish;
1160 }
1161 *list = l;
1162 mod->ignorecmd = true;
1163 } else
1164 err = __kmod_module_fill_softdep(mod, list);
1165
Lucas De Marchib1a51252012-01-29 01:49:09 -02001166finish:
1167 kmod_module_unref_list(dep);
1168 return err;
1169}
1170
1171static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001172 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001173 struct kmod_list **list)
1174{
1175 int err;
1176
1177 assert(mod != NULL);
1178 assert(list != NULL && *list == NULL);
1179
1180 /*
1181 * Make sure we don't get screwed by previous calls to this function
1182 */
1183 kmod_set_modules_visited(mod->ctx, false);
Michal Marek450bd1b2014-03-31 15:18:50 +02001184 kmod_set_modules_required(mod->ctx, false);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001185
Michal Marek450bd1b2014-03-31 15:18:50 +02001186 err = __kmod_module_get_probe_list(mod, true, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001187 if (err < 0) {
1188 kmod_module_unref_list(*list);
1189 *list = NULL;
1190 }
1191
1192 return err;
1193}
1194
Lucas De Marchiddbda022011-12-27 11:40:10 -02001195/**
1196 * kmod_module_probe_insert_module:
1197 * @mod: kmod module
1198 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +08001199 * behavior of this function, valid flags are
1200 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1201 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1202 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1203 * commands and softdeps configured in the system;
1204 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1205 * live in kernel or not;
1206 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1207 * associated callback function;
1208 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1209 * and the module is already live in kernel, the function will fail if this
1210 * flag is specified;
1211 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1212 * filter to this module and its dependencies. If any of the dependencies (or
1213 * the module) is blacklisted, the probe will fail, unless the blacklisted
1214 * module is already live in kernel;
1215 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1216 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1217 * alias and is blacklisted.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001218 * @extra_options: module's options to pass to Linux Kernel. It applies only
1219 * to @mod, not to its dependencies.
1220 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001221 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001222 * @print_action: function to call with the action being taken (install or
1223 * insmod). It's useful for tools like modprobe when running with verbose
1224 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001225 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001226 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001227 * install commands and applying blacklist.
1228 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001229 * If @run_install is NULL, this function will fork and exec by calling
1230 * system(3). Don't pass a NULL argument in @run_install if your binary is
1231 * setuid/setgid (see warning in system(3)). If you need control over the
1232 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001233 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001234 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1235 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001236 */
1237KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1238 unsigned int flags, const char *extra_options,
1239 int (*run_install)(struct kmod_module *m,
1240 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001241 const void *data,
1242 void (*print_action)(struct kmod_module *m,
1243 bool install,
1244 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001245{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001246 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001247 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001248 int err;
1249
1250 if (mod == NULL)
1251 return -ENOENT;
1252
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001253 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1254 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001255 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001256 return -EEXIST;
1257 else
1258 return 0;
1259 }
1260
Lucas De Marchi68820172012-08-17 09:38:05 -03001261 /*
1262 * Ugly assignement + check. We need to check if we were told to check
1263 * blacklist and also return the reason why we failed.
1264 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1265 * module is an alias, so we also need to check it
1266 */
1267 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1268 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1269 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001270 if (module_is_blacklisted(mod))
1271 return err;
1272 }
1273
Lucas De Marchi89e92482012-01-29 02:35:46 -02001274 err = kmod_module_get_probe_list(mod,
1275 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001276 if (err < 0)
1277 return err;
1278
1279 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1280 struct kmod_list *filtered = NULL;
1281
Dave Reisnerd80b1032012-02-24 10:05:11 -05001282 err = kmod_module_apply_filter(mod->ctx,
1283 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001284 if (err < 0)
1285 return err;
1286
1287 kmod_module_unref_list(list);
1288 if (filtered == NULL)
1289 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1290
1291 list = filtered;
1292 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001293
1294 cb.run_install = run_install;
1295 cb.data = (void *) data;
1296
Lucas De Marchib1a51252012-01-29 01:49:09 -02001297 kmod_list_foreach(l, list) {
1298 struct kmod_module *m = l->data;
1299 const char *moptions = kmod_module_get_options(m);
1300 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001301 char *options;
1302
1303 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1304 && module_is_inkernel(m)) {
1305 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1306 m->name);
1307 err = -EEXIST;
1308 goto finish_module;
1309 }
1310
1311 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001312 m == mod ? extra_options : NULL);
1313
Lucas De Marchi89e92482012-01-29 02:35:46 -02001314 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001315 if (print_action != NULL)
1316 print_action(m, true, options ?: "");
1317
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001318 if (!(flags & KMOD_PROBE_DRY_RUN))
1319 err = module_do_install_commands(m, options,
1320 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001321 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001322 if (print_action != NULL)
1323 print_action(m, false, options ?: "");
1324
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001325 if (!(flags & KMOD_PROBE_DRY_RUN))
1326 err = kmod_module_insert_module(m, flags,
1327 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001328 }
1329
1330 free(options);
1331
Lucas De Marchiabd55572012-02-19 04:20:30 -02001332finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001333 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001334 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001335 * already loaded and the module being loaded is not a softdep
1336 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001337 *
1338 * We need to check here because of race conditions. We
1339 * checked first if module was already loaded but it may have
1340 * been loaded between the check and the moment we try to
1341 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001342 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001343 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001344 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001345 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001346
Michal Marek450bd1b2014-03-31 15:18:50 +02001347 /*
1348 * Ignore errors from softdeps
1349 */
1350 if (err == -EEXIST || !m->required)
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001351 err = 0;
Michal Marek450bd1b2014-03-31 15:18:50 +02001352
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001353 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001354 break;
1355 }
1356
1357 kmod_module_unref_list(list);
1358 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001359}
1360
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001361/**
1362 * kmod_module_get_options:
1363 * @mod: kmod module
1364 *
1365 * Get options of this kmod module. Options come from the configuration file
1366 * and are cached in @mod. The first call to this function will search for
1367 * this module in configuration and subsequent calls return the cached string.
1368 *
1369 * Returns: a string with all the options separated by spaces. This string is
1370 * owned by @mod, do not free it.
1371 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001372KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1373{
1374 if (mod == NULL)
1375 return NULL;
1376
1377 if (!mod->init.options) {
1378 /* lazy init */
1379 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001380 const struct kmod_list *l;
1381 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001382 char *opts = NULL;
1383 size_t optslen = 0;
1384
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001385 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001386
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001387 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001388 const char *modname = kmod_option_get_modname(l);
1389 const char *str;
1390 size_t len;
1391 void *tmp;
1392
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001393 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1394 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1395 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001396 continue;
1397
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001398 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 -02001399 str = kmod_option_get_options(l);
1400 len = strlen(str);
1401 if (len < 1)
1402 continue;
1403
1404 tmp = realloc(opts, optslen + len + 2);
1405 if (tmp == NULL) {
1406 free(opts);
1407 goto failed;
1408 }
1409
1410 opts = tmp;
1411
1412 if (optslen > 0) {
1413 opts[optslen] = ' ';
1414 optslen++;
1415 }
1416
1417 memcpy(opts + optslen, str, len);
1418 optslen += len;
1419 opts[optslen] = '\0';
1420 }
1421
1422 m->init.options = true;
1423 m->options = opts;
1424 }
1425
1426 return mod->options;
1427
1428failed:
1429 ERR(mod->ctx, "out of memory\n");
1430 return NULL;
1431}
1432
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001433/**
1434 * kmod_module_get_install_commands:
1435 * @mod: kmod module
1436 *
1437 * Get install commands for this kmod module. Install commands come from the
1438 * configuration file and are cached in @mod. The first call to this function
1439 * will search for this module in configuration and subsequent calls return
1440 * the cached string. The install commands are returned as they were in the
1441 * configuration, concatenated by ';'. No other processing is made in this
1442 * string.
1443 *
1444 * Returns: a string with all install commands separated by semicolons. This
1445 * string is owned by @mod, do not free it.
1446 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001447KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1448{
1449 if (mod == NULL)
1450 return NULL;
1451
1452 if (!mod->init.install_commands) {
1453 /* lazy init */
1454 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001455 const struct kmod_list *l;
1456 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001457
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001458 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001459
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001460 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001461 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001462
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001463 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001464 continue;
1465
Lucas De Marchi60f67602011-12-16 03:33:26 -02001466 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001467
Lucas De Marchi60f67602011-12-16 03:33:26 -02001468 /*
1469 * find only the first command, as modprobe from
1470 * module-init-tools does
1471 */
1472 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001473 }
1474
1475 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001476 }
1477
1478 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001479}
1480
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001481void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1482{
1483 mod->init.install_commands = true;
1484 mod->install_commands = cmd;
1485}
1486
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001487static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1488{
1489 struct kmod_list *ret = NULL;
1490 unsigned i;
1491
1492 for (i = 0; i < count; i++) {
1493 const char *depname = array[i];
1494 struct kmod_list *lst = NULL;
1495 int err;
1496
1497 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1498 if (err < 0) {
1499 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1500 continue;
1501 } else if (lst != NULL)
1502 ret = kmod_list_append_list(ret, lst);
1503 }
1504 return ret;
1505}
1506
1507/**
1508 * kmod_module_get_softdeps:
1509 * @mod: kmod module
1510 * @pre: where to save the list of preceding soft dependencies.
1511 * @post: where to save the list of post soft dependencies.
1512 *
1513 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001514 * from configuration file and are not cached in @mod because it may include
1515 * dependency cycles that would make we leak kmod_module. Any call
1516 * to this function will search for this module in configuration, allocate a
1517 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001518 *
1519 * Both @pre and @post are newly created list of kmod_module and
1520 * should be unreferenced with kmod_module_unref_list().
1521 *
1522 * Returns: 0 on success or < 0 otherwise.
1523 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001524KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1525 struct kmod_list **pre,
1526 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001527{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001528 const struct kmod_list *l;
1529 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001530
1531 if (mod == NULL || pre == NULL || post == NULL)
1532 return -ENOENT;
1533
1534 assert(*pre == NULL);
1535 assert(*post == NULL);
1536
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001537 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001538
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001539 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001540 const char *modname = kmod_softdep_get_name(l);
1541 const char * const *array;
1542 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001543
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001544 if (fnmatch(modname, mod->name, 0) != 0)
1545 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001546
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001547 array = kmod_softdep_get_pre(l, &count);
1548 *pre = lookup_softdep(mod->ctx, array, count);
1549 array = kmod_softdep_get_post(l, &count);
1550 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001551
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001552 /*
1553 * find only the first command, as modprobe from
1554 * module-init-tools does
1555 */
1556 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001557 }
1558
1559 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001560}
1561
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001562/**
1563 * kmod_module_get_remove_commands:
1564 * @mod: kmod module
1565 *
1566 * Get remove commands for this kmod module. Remove commands come from the
1567 * configuration file and are cached in @mod. The first call to this function
1568 * will search for this module in configuration and subsequent calls return
1569 * the cached string. The remove commands are returned as they were in the
1570 * configuration, concatenated by ';'. No other processing is made in this
1571 * string.
1572 *
1573 * Returns: a string with all remove commands separated by semicolons. This
1574 * string is owned by @mod, do not free it.
1575 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001576KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1577{
1578 if (mod == NULL)
1579 return NULL;
1580
1581 if (!mod->init.remove_commands) {
1582 /* lazy init */
1583 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001584 const struct kmod_list *l;
1585 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001586
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001587 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001588
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001589 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001590 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001591
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001592 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001593 continue;
1594
Lucas De Marchi60f67602011-12-16 03:33:26 -02001595 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001596
Lucas De Marchi60f67602011-12-16 03:33:26 -02001597 /*
1598 * find only the first command, as modprobe from
1599 * module-init-tools does
1600 */
1601 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001602 }
1603
1604 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001605 }
1606
1607 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001608}
1609
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001610void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1611{
1612 mod->init.remove_commands = true;
1613 mod->remove_commands = cmd;
1614}
1615
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001616/**
1617 * SECTION:libkmod-loaded
1618 * @short_description: currently loaded modules
1619 *
1620 * Information about currently loaded modules, as reported by Linux kernel.
1621 * These information are not cached by libkmod and are always read from /sys
1622 * and /proc/modules.
1623 */
1624
1625/**
1626 * kmod_module_new_from_loaded:
1627 * @ctx: kmod library context
1628 * @list: where to save the list of loaded modules
1629 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001630 * Create a new list of kmod modules with all modules currently loaded in
1631 * kernel. It uses /proc/modules to get the names of loaded modules and to
1632 * create kmod modules by calling kmod_module_new_from_name() in each of them.
Chengwei Yang491c4902013-05-04 17:07:02 +08001633 * They are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001634 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001635 * The initial refcount is 1, and needs to be decremented to release the
1636 * resources of the kmod_module. The returned @list must be released by
1637 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1638 * kmod_modules created, they are all released upon @ctx destruction too. Do
1639 * not unref @ctx before all the desired operations with the returned list are
1640 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001641 *
1642 * Returns: 0 on success or < 0 on error.
1643 */
1644KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1645 struct kmod_list **list)
1646{
1647 struct kmod_list *l = NULL;
1648 FILE *fp;
1649 char line[4096];
1650
1651 if (ctx == NULL || list == NULL)
1652 return -ENOENT;
1653
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001654 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001655 if (fp == NULL) {
1656 int err = -errno;
1657 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1658 return err;
1659 }
1660
1661 while (fgets(line, sizeof(line), fp)) {
1662 struct kmod_module *m;
1663 struct kmod_list *node;
1664 int err;
Michal Marek2206d7f2016-06-17 16:04:15 +02001665 size_t len = strlen(line);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001666 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1667
1668 err = kmod_module_new_from_name(ctx, name, &m);
1669 if (err < 0) {
1670 ERR(ctx, "could not get module from name '%s': %s\n",
1671 name, strerror(-err));
Michal Marek2206d7f2016-06-17 16:04:15 +02001672 goto eat_line;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001673 }
1674
1675 node = kmod_list_append(l, m);
1676 if (node)
1677 l = node;
1678 else {
1679 ERR(ctx, "out of memory\n");
1680 kmod_module_unref(m);
1681 }
Michal Marek2206d7f2016-06-17 16:04:15 +02001682eat_line:
1683 while (line[len - 1] != '\n' && fgets(line, sizeof(line), fp))
1684 len = strlen(line);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001685 }
1686
1687 fclose(fp);
1688 *list = l;
1689
1690 return 0;
1691}
1692
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001693/**
1694 * kmod_module_initstate_str:
1695 * @state: the state as returned by kmod_module_get_initstate()
1696 *
1697 * Translate a initstate to a string.
1698 *
1699 * Returns: the string associated to the @state. This string is statically
1700 * allocated, do not free it.
1701 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001702KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1703{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001704 switch (state) {
1705 case KMOD_MODULE_BUILTIN:
1706 return "builtin";
1707 case KMOD_MODULE_LIVE:
1708 return "live";
1709 case KMOD_MODULE_COMING:
1710 return "coming";
1711 case KMOD_MODULE_GOING:
1712 return "going";
1713 default:
1714 return NULL;
1715 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001716}
1717
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001718/**
1719 * kmod_module_get_initstate:
1720 * @mod: kmod module
1721 *
1722 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1723 * /sys filesystem.
1724 *
Chengwei Yangd7152f62013-05-04 17:07:03 +08001725 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1726 * KMOD_MODULE_BUILTIN: module is builtin;
1727 * KMOD_MODULE_LIVE: module is live in kernel;
1728 * KMOD_MODULE_COMING: module is being loaded;
1729 * KMOD_MODULE_GOING: module is being unloaded.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001730 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001731KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1732{
1733 char path[PATH_MAX], buf[32];
1734 int fd, err, pathlen;
1735
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001736 if (mod == NULL)
1737 return -ENOENT;
1738
Harish Jenny K Nfd44a982015-02-22 15:41:07 -03001739 /* remove const: this can only change internal state */
1740 if (kmod_module_is_builtin((struct kmod_module *)mod))
Lucas De Marchi38052742012-02-16 20:43:16 -02001741 return KMOD_MODULE_BUILTIN;
1742
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001743 pathlen = snprintf(path, sizeof(path),
1744 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001745 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001746 if (fd < 0) {
1747 err = -errno;
1748
Lucas De Marchiddbda022011-12-27 11:40:10 -02001749 DBG(mod->ctx, "could not open '%s': %s\n",
1750 path, strerror(-err));
1751
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001752 if (pathlen > (int)sizeof("/initstate") - 1) {
1753 struct stat st;
1754 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1755 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
Harish Jenny K Nfd44a982015-02-22 15:41:07 -03001756 return KMOD_MODULE_COMING;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001757 }
1758
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001759 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001760 path, strerror(-err));
1761 return err;
1762 }
1763
1764 err = read_str_safe(fd, buf, sizeof(buf));
1765 close(fd);
1766 if (err < 0) {
1767 ERR(mod->ctx, "could not read from '%s': %s\n",
1768 path, strerror(-err));
1769 return err;
1770 }
1771
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001772 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001773 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001774 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001775 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001776 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001777 return KMOD_MODULE_GOING;
1778
1779 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1780 return -EINVAL;
1781}
1782
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001783/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001784 * kmod_module_get_size:
1785 * @mod: kmod module
1786 *
Dave Reisner486f9012012-06-28 09:09:48 -04001787 * Get the size of this kmod module as returned by Linux kernel. If supported,
1788 * the size is read from the coresize attribute in /sys/module. For older
1789 * kernels, this falls back on /proc/modules and searches for the specified
1790 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001791 *
1792 * Returns: the size of this kmod module.
1793 */
1794KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1795{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001796 FILE *fp;
1797 char line[4096];
1798 int lineno = 0;
1799 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001800 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001801
1802 if (mod == NULL)
1803 return -ENOENT;
1804
Dave Reisner486f9012012-06-28 09:09:48 -04001805 /* try to open the module dir in /sys. If this fails, don't
1806 * bother trying to find the size as we know the module isn't
1807 * loaded.
1808 */
1809 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
Cristian Rodríguez74c26942014-06-18 20:51:00 -04001810 dfd = open(line, O_RDONLY|O_CLOEXEC);
Dave Reisner486f9012012-06-28 09:09:48 -04001811 if (dfd < 0)
1812 return -errno;
1813
1814 /* available as of linux 3.3.x */
1815 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1816 if (cfd >= 0) {
1817 if (read_str_long(cfd, &size, 10) < 0)
1818 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1819 close(cfd);
1820 goto done;
1821 }
1822
1823 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001824 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001825 if (fp == NULL) {
1826 int err = -errno;
1827 ERR(mod->ctx,
1828 "could not open /proc/modules: %s\n", strerror(errno));
Leandro Pereira30bfd482014-04-28 21:04:48 -03001829 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001830 return err;
1831 }
1832
1833 while (fgets(line, sizeof(line), fp)) {
Michal Marek2206d7f2016-06-17 16:04:15 +02001834 size_t len = strlen(line);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001835 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1836 long value;
1837
1838 lineno++;
1839 if (tok == NULL || !streq(tok, mod->name))
Michal Marek2206d7f2016-06-17 16:04:15 +02001840 goto eat_line;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001841
1842 tok = strtok_r(NULL, " \t", &saveptr);
1843 if (tok == NULL) {
1844 ERR(mod->ctx,
1845 "invalid line format at /proc/modules:%d\n", lineno);
1846 break;
1847 }
1848
1849 value = strtol(tok, &endptr, 10);
1850 if (endptr == tok || *endptr != '\0') {
1851 ERR(mod->ctx,
1852 "invalid line format at /proc/modules:%d\n", lineno);
1853 break;
1854 }
1855
1856 size = value;
1857 break;
Michal Marek2206d7f2016-06-17 16:04:15 +02001858eat_line:
1859 while (line[len - 1] != '\n' && fgets(line, sizeof(line), fp))
1860 len = strlen(line);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001861 }
1862 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001863
1864done:
1865 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001866 return size;
1867}
1868
1869/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001870 * kmod_module_get_refcnt:
1871 * @mod: kmod module
1872 *
1873 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1874 * /sys filesystem.
1875 *
Peter Wu19308992016-05-21 13:15:19 +02001876 * Returns: the reference count on success or < 0 on failure.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001877 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001878KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1879{
1880 char path[PATH_MAX];
1881 long refcnt;
1882 int fd, err;
1883
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001884 if (mod == NULL)
1885 return -ENOENT;
1886
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001887 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001888 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001889 if (fd < 0) {
1890 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001891 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001892 path, strerror(errno));
1893 return err;
1894 }
1895
1896 err = read_str_long(fd, &refcnt, 10);
1897 close(fd);
1898 if (err < 0) {
1899 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1900 path, strerror(-err));
1901 return err;
1902 }
1903
1904 return (int)refcnt;
1905}
1906
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001907/**
1908 * kmod_module_get_holders:
1909 * @mod: kmod module
1910 *
1911 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1912 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1913 *
1914 * Returns: a new list of kmod modules on success or NULL on failure.
1915 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001916KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1917{
1918 char dname[PATH_MAX];
1919 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001920 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001921 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001922
Lucas De Marchib9a7da32013-04-23 20:47:28 -03001923 if (mod == NULL || mod->ctx == NULL)
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001924 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001925
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001926 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1927
1928 d = opendir(dname);
1929 if (d == NULL) {
1930 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001931 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001932 return NULL;
1933 }
1934
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001935 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001936 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001937 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001938 int err;
1939
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001940 if (dent->d_name[0] == '.') {
1941 if (dent->d_name[1] == '\0' ||
1942 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001943 continue;
1944 }
1945
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001946 err = kmod_module_new_from_name(mod->ctx, dent->d_name,
1947 &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001948 if (err < 0) {
1949 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001950 dent->d_name, strerror(-err));
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001951 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001952 }
1953
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001954 l = kmod_list_append(list, holder);
1955 if (l != NULL) {
1956 list = l;
1957 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001958 ERR(mod->ctx, "out of memory\n");
1959 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001960 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001961 }
1962 }
1963
1964 closedir(d);
1965 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001966
1967fail:
1968 closedir(d);
1969 kmod_module_unref_list(list);
1970 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001971}
1972
1973struct kmod_module_section {
1974 unsigned long address;
1975 char name[];
1976};
1977
1978static void kmod_module_section_free(struct kmod_module_section *section)
1979{
1980 free(section);
1981}
1982
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001983/**
1984 * kmod_module_get_sections:
1985 * @mod: kmod module
1986 *
1987 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1988 * structure contained in this list is internal to libkmod and their fields
1989 * can be obtained by calling kmod_module_section_get_name() and
1990 * kmod_module_section_get_address().
1991 *
1992 * After use, free the @list by calling kmod_module_section_free_list().
1993 *
1994 * Returns: a new list of kmod module sections on success or NULL on failure.
1995 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001996KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1997{
1998 char dname[PATH_MAX];
1999 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002000 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002001 DIR *d;
2002 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002003
2004 if (mod == NULL)
2005 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002006
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002007 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
2008
2009 d = opendir(dname);
2010 if (d == NULL) {
2011 ERR(mod->ctx, "could not open '%s': %s\n",
2012 dname, strerror(errno));
2013 return NULL;
2014 }
2015
2016 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002017
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002018 for (dent = readdir(d); dent; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002019 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002020 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002021 unsigned long address;
2022 size_t namesz;
2023 int fd, err;
2024
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002025 if (dent->d_name[0] == '.') {
2026 if (dent->d_name[1] == '\0' ||
2027 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002028 continue;
2029 }
2030
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002031 fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002032 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002033 ERR(mod->ctx, "could not open '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002034 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002035 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002036 }
2037
2038 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002039 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002040 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002041 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002042 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002043 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002044 }
2045
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002046 namesz = strlen(dent->d_name) + 1;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002047 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002048
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002049 if (section == NULL) {
2050 ERR(mod->ctx, "out of memory\n");
2051 goto fail;
2052 }
2053
2054 section->address = address;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002055 memcpy(section->name, dent->d_name, namesz);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002056
2057 l = kmod_list_append(list, section);
2058 if (l != NULL) {
2059 list = l;
2060 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002061 ERR(mod->ctx, "out of memory\n");
2062 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002063 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002064 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002065 }
2066
2067 closedir(d);
2068 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002069
2070fail:
2071 closedir(d);
2072 kmod_module_unref_list(list);
2073 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002074}
2075
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002076/**
2077 * kmod_module_section_get_module_name:
2078 * @entry: a list entry representing a kmod module section
2079 *
2080 * Get the name of a kmod module section.
2081 *
2082 * After use, free the @list by calling kmod_module_section_free_list().
2083 *
2084 * Returns: the name of this kmod module section on success or NULL on
2085 * failure. The string is owned by the section, do not free it.
2086 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002087KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2088{
2089 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002090
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002091 if (entry == NULL)
2092 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002093
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002094 section = entry->data;
2095 return section->name;
2096}
2097
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002098/**
2099 * kmod_module_section_get_address:
2100 * @entry: a list entry representing a kmod module section
2101 *
2102 * Get the address of a kmod module section.
2103 *
2104 * After use, free the @list by calling kmod_module_section_free_list().
2105 *
2106 * Returns: the address of this kmod module section on success or ULONG_MAX
2107 * on failure.
2108 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002109KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2110{
2111 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002112
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002113 if (entry == NULL)
2114 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002115
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002116 section = entry->data;
2117 return section->address;
2118}
2119
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002120/**
2121 * kmod_module_section_free_list:
2122 * @list: kmod module section list
2123 *
2124 * Release the resources taken by @list
2125 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002126KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2127{
2128 while (list) {
2129 kmod_module_section_free(list->data);
2130 list = kmod_list_remove(list);
2131 }
2132}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002133
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002134static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2135{
2136 if (mod->file == NULL) {
2137 const char *path = kmod_module_get_path(mod);
2138
2139 if (path == NULL) {
2140 errno = ENOENT;
2141 return NULL;
2142 }
2143
2144 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2145 path);
2146 if (mod->file == NULL)
2147 return NULL;
2148 }
2149
2150 return kmod_file_get_elf(mod->file);
2151}
2152
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002153struct kmod_module_info {
2154 char *key;
2155 char value[];
2156};
2157
2158static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2159{
2160 struct kmod_module_info *info;
2161
2162 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2163 if (info == NULL)
2164 return NULL;
2165
2166 info->key = (char *)info + sizeof(struct kmod_module_info)
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002167 + valuelen + 1;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002168 memcpy(info->key, key, keylen);
2169 info->key[keylen] = '\0';
2170 memcpy(info->value, value, valuelen);
2171 info->value[valuelen] = '\0';
2172 return info;
2173}
2174
2175static void kmod_module_info_free(struct kmod_module_info *info)
2176{
2177 free(info);
2178}
2179
Michal Marekf64458c2013-01-16 10:18:17 +01002180static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2181{
2182 struct kmod_module_info *info;
2183 struct kmod_list *n;
2184
2185 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002186 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002187 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002188 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002189 if (n != NULL)
2190 *list = n;
2191 else
Michal Marekf64458c2013-01-16 10:18:17 +01002192 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002193 return n;
2194}
2195
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002196/**
2197 * kmod_module_get_info:
2198 * @mod: kmod module
2199 * @list: where to return list of module information. Use
2200 * kmod_module_info_get_key() and
2201 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002202 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002203 *
2204 * Get a list of entries in ELF section ".modinfo", these contain
2205 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002206 * values. If the module is signed (CONFIG_MODULE_SIG), information
2207 * about the module signature is included as well: signer,
2208 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002209 *
2210 * After use, free the @list by calling kmod_module_info_free_list().
2211 *
2212 * Returns: 0 on success or < 0 otherwise.
2213 */
2214KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2215{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002216 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002217 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002218 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002219 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002220
2221 if (mod == NULL || list == NULL)
2222 return -ENOENT;
2223
2224 assert(*list == NULL);
2225
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002226 elf = kmod_module_get_elf(mod);
2227 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002228 return -errno;
2229
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002230 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002231 if (count < 0)
2232 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002233
2234 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002235 struct kmod_list *n;
2236 const char *key, *value;
2237 size_t keylen, valuelen;
2238
2239 key = strings[i];
2240 value = strchr(key, '=');
2241 if (value == NULL) {
2242 keylen = strlen(key);
2243 valuelen = 0;
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002244 value = key;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002245 } else {
2246 keylen = value - key;
2247 value++;
2248 valuelen = strlen(value);
2249 }
2250
Michal Marekf64458c2013-01-16 10:18:17 +01002251 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2252 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002253 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002254 }
Michal Marek8fe16812013-01-16 09:52:01 +01002255
2256 if (kmod_module_signature_info(mod->file, &sig_info)) {
2257 struct kmod_list *n;
2258 char *key_hex;
2259
Lucas De Marchie78fe152016-06-05 00:09:22 -03002260 n = kmod_module_info_append(list, "signature", strlen("sig_id"),
2261 sig_info.id_type, strlen(sig_info.id_type));
2262 if (n == NULL)
2263 goto list_error;
2264 count++;
2265
Michal Marek8fe16812013-01-16 09:52:01 +01002266 n = kmod_module_info_append(list, "signer", strlen("signer"),
2267 sig_info.signer, sig_info.signer_len);
2268 if (n == NULL)
2269 goto list_error;
2270 count++;
2271
Lucas De Marchidcdb1772016-06-04 22:37:11 -03002272 if (sig_info.key_id_len) {
2273 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2274 key_hex = malloc(sig_info.key_id_len * 3);
2275 if (key_hex == NULL)
2276 goto list_error;
2277 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2278 sprintf(key_hex + i * 3, "%02X",
2279 (unsigned char)sig_info.key_id[i]);
2280 if (i < (int)sig_info.key_id_len - 1)
2281 key_hex[i * 3 + 2] = ':';
2282 }
2283 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2284 key_hex, sig_info.key_id_len * 3 - 1);
2285 free(key_hex);
2286 if (n == NULL)
2287 goto list_error;
2288 count++;
2289 } else {
2290 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2291 NULL, 0);
2292 if (n == NULL)
2293 goto list_error;
2294 count++;
Michal Marek8fe16812013-01-16 09:52:01 +01002295 }
Michal Marek8fe16812013-01-16 09:52:01 +01002296
2297 n = kmod_module_info_append(list,
2298 "sig_hashalgo", strlen("sig_hashalgo"),
2299 sig_info.hash_algo, strlen(sig_info.hash_algo));
2300 if (n == NULL)
2301 goto list_error;
2302 count++;
2303
2304 /*
Lucas De Marchie78fe152016-06-05 00:09:22 -03002305 * Omit sig_info.algo for now, as these
Michal Marek8fe16812013-01-16 09:52:01 +01002306 * are currently constant.
2307 */
2308 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002309 ret = count;
2310
2311list_error:
Michal Marek63339342013-01-16 17:51:57 +01002312 if (ret < 0) {
2313 kmod_module_info_free_list(*list);
2314 *list = NULL;
2315 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002316 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002317 return ret;
2318}
2319
2320/**
2321 * kmod_module_info_get_key:
2322 * @entry: a list entry representing a kmod module info
2323 *
2324 * Get the key of a kmod module info.
2325 *
2326 * Returns: the key of this kmod module info on success or NULL on
2327 * failure. The string is owned by the info, do not free it.
2328 */
2329KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2330{
2331 struct kmod_module_info *info;
2332
2333 if (entry == NULL)
2334 return NULL;
2335
2336 info = entry->data;
2337 return info->key;
2338}
2339
2340/**
2341 * kmod_module_info_get_value:
2342 * @entry: a list entry representing a kmod module info
2343 *
2344 * Get the value of a kmod module info.
2345 *
2346 * Returns: the value of this kmod module info on success or NULL on
2347 * failure. The string is owned by the info, do not free it.
2348 */
2349KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2350{
2351 struct kmod_module_info *info;
2352
2353 if (entry == NULL)
2354 return NULL;
2355
2356 info = entry->data;
2357 return info->value;
2358}
2359
2360/**
2361 * kmod_module_info_free_list:
2362 * @list: kmod module info list
2363 *
2364 * Release the resources taken by @list
2365 */
2366KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2367{
2368 while (list) {
2369 kmod_module_info_free(list->data);
2370 list = kmod_list_remove(list);
2371 }
2372}
2373
2374struct kmod_module_version {
2375 uint64_t crc;
2376 char symbol[];
2377};
2378
2379static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2380{
2381 struct kmod_module_version *mv;
2382 size_t symbollen = strlen(symbol) + 1;
2383
2384 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2385 if (mv == NULL)
2386 return NULL;
2387
2388 mv->crc = crc;
2389 memcpy(mv->symbol, symbol, symbollen);
2390 return mv;
2391}
2392
2393static void kmod_module_version_free(struct kmod_module_version *version)
2394{
2395 free(version);
2396}
2397
2398/**
2399 * kmod_module_get_versions:
2400 * @mod: kmod module
2401 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002402 * kmod_module_version_get_symbol() and
2403 * kmod_module_version_get_crc(). Release this list with
2404 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002405 *
2406 * Get a list of entries in ELF section "__versions".
2407 *
2408 * After use, free the @list by calling kmod_module_versions_free_list().
2409 *
2410 * Returns: 0 on success or < 0 otherwise.
2411 */
2412KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2413{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002414 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002415 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002416 int i, count, ret = 0;
2417
2418 if (mod == NULL || list == NULL)
2419 return -ENOENT;
2420
2421 assert(*list == NULL);
2422
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002423 elf = kmod_module_get_elf(mod);
2424 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002425 return -errno;
2426
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002427 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002428 if (count < 0)
2429 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002430
2431 for (i = 0; i < count; i++) {
2432 struct kmod_module_version *mv;
2433 struct kmod_list *n;
2434
2435 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2436 if (mv == NULL) {
2437 ret = -errno;
2438 kmod_module_versions_free_list(*list);
2439 *list = NULL;
2440 goto list_error;
2441 }
2442
2443 n = kmod_list_append(*list, mv);
2444 if (n != NULL)
2445 *list = n;
2446 else {
2447 kmod_module_version_free(mv);
2448 kmod_module_versions_free_list(*list);
2449 *list = NULL;
2450 ret = -ENOMEM;
2451 goto list_error;
2452 }
2453 }
2454 ret = count;
2455
2456list_error:
2457 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002458 return ret;
2459}
2460
2461/**
Chengwei Yang491c4902013-05-04 17:07:02 +08002462 * kmod_module_version_get_symbol:
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002463 * @entry: a list entry representing a kmod module versions
2464 *
2465 * Get the symbol of a kmod module versions.
2466 *
2467 * Returns: the symbol of this kmod module versions on success or NULL
2468 * on failure. The string is owned by the versions, do not free it.
2469 */
2470KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2471{
2472 struct kmod_module_version *version;
2473
2474 if (entry == NULL)
2475 return NULL;
2476
2477 version = entry->data;
2478 return version->symbol;
2479}
2480
2481/**
2482 * kmod_module_version_get_crc:
2483 * @entry: a list entry representing a kmod module version
2484 *
2485 * Get the crc of a kmod module version.
2486 *
2487 * Returns: the crc of this kmod module version on success or NULL on
2488 * failure. The string is owned by the version, do not free it.
2489 */
2490KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2491{
2492 struct kmod_module_version *version;
2493
2494 if (entry == NULL)
2495 return 0;
2496
2497 version = entry->data;
2498 return version->crc;
2499}
2500
2501/**
2502 * kmod_module_versions_free_list:
2503 * @list: kmod module versions list
2504 *
2505 * Release the resources taken by @list
2506 */
2507KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2508{
2509 while (list) {
2510 kmod_module_version_free(list->data);
2511 list = kmod_list_remove(list);
2512 }
2513}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002514
2515struct kmod_module_symbol {
2516 uint64_t crc;
2517 char symbol[];
2518};
2519
2520static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2521{
2522 struct kmod_module_symbol *mv;
2523 size_t symbollen = strlen(symbol) + 1;
2524
2525 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2526 if (mv == NULL)
2527 return NULL;
2528
2529 mv->crc = crc;
2530 memcpy(mv->symbol, symbol, symbollen);
2531 return mv;
2532}
2533
2534static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2535{
2536 free(symbol);
2537}
2538
2539/**
2540 * kmod_module_get_symbols:
2541 * @mod: kmod module
2542 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002543 * kmod_module_symbol_get_symbol() and
2544 * kmod_module_symbol_get_crc(). Release this list with
2545 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002546 *
2547 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2548 *
2549 * After use, free the @list by calling kmod_module_symbols_free_list().
2550 *
2551 * Returns: 0 on success or < 0 otherwise.
2552 */
2553KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2554{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002555 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002556 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002557 int i, count, ret = 0;
2558
2559 if (mod == NULL || list == NULL)
2560 return -ENOENT;
2561
2562 assert(*list == NULL);
2563
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002564 elf = kmod_module_get_elf(mod);
2565 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002566 return -errno;
2567
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002568 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002569 if (count < 0)
2570 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002571
2572 for (i = 0; i < count; i++) {
2573 struct kmod_module_symbol *mv;
2574 struct kmod_list *n;
2575
2576 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2577 if (mv == NULL) {
2578 ret = -errno;
2579 kmod_module_symbols_free_list(*list);
2580 *list = NULL;
2581 goto list_error;
2582 }
2583
2584 n = kmod_list_append(*list, mv);
2585 if (n != NULL)
2586 *list = n;
2587 else {
2588 kmod_module_symbol_free(mv);
2589 kmod_module_symbols_free_list(*list);
2590 *list = NULL;
2591 ret = -ENOMEM;
2592 goto list_error;
2593 }
2594 }
2595 ret = count;
2596
2597list_error:
2598 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002599 return ret;
2600}
2601
2602/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002603 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002604 * @entry: a list entry representing a kmod module symbols
2605 *
2606 * Get the symbol of a kmod module symbols.
2607 *
2608 * Returns: the symbol of this kmod module symbols on success or NULL
2609 * on failure. The string is owned by the symbols, do not free it.
2610 */
2611KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2612{
2613 struct kmod_module_symbol *symbol;
2614
2615 if (entry == NULL)
2616 return NULL;
2617
2618 symbol = entry->data;
2619 return symbol->symbol;
2620}
2621
2622/**
2623 * kmod_module_symbol_get_crc:
2624 * @entry: a list entry representing a kmod module symbol
2625 *
2626 * Get the crc of a kmod module symbol.
2627 *
2628 * Returns: the crc of this kmod module symbol on success or NULL on
2629 * failure. The string is owned by the symbol, do not free it.
2630 */
2631KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2632{
2633 struct kmod_module_symbol *symbol;
2634
2635 if (entry == NULL)
2636 return 0;
2637
2638 symbol = entry->data;
2639 return symbol->crc;
2640}
2641
2642/**
2643 * kmod_module_symbols_free_list:
2644 * @list: kmod module symbols list
2645 *
2646 * Release the resources taken by @list
2647 */
2648KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2649{
2650 while (list) {
2651 kmod_module_symbol_free(list->data);
2652 list = kmod_list_remove(list);
2653 }
2654}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002655
2656struct kmod_module_dependency_symbol {
2657 uint64_t crc;
2658 uint8_t bind;
2659 char symbol[];
2660};
2661
2662static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2663{
2664 struct kmod_module_dependency_symbol *mv;
2665 size_t symbollen = strlen(symbol) + 1;
2666
2667 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2668 if (mv == NULL)
2669 return NULL;
2670
2671 mv->crc = crc;
2672 mv->bind = bind;
2673 memcpy(mv->symbol, symbol, symbollen);
2674 return mv;
2675}
2676
2677static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2678{
2679 free(dependency_symbol);
2680}
2681
2682/**
2683 * kmod_module_get_dependency_symbols:
2684 * @mod: kmod module
2685 * @list: where to return list of module dependency_symbols. Use
2686 * kmod_module_dependency_symbol_get_symbol() and
2687 * kmod_module_dependency_symbol_get_crc(). Release this list with
2688 * kmod_module_dependency_symbols_free_list()
2689 *
2690 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2691 *
2692 * After use, free the @list by calling
2693 * kmod_module_dependency_symbols_free_list().
2694 *
2695 * Returns: 0 on success or < 0 otherwise.
2696 */
2697KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2698{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002699 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002700 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002701 int i, count, ret = 0;
2702
2703 if (mod == NULL || list == NULL)
2704 return -ENOENT;
2705
2706 assert(*list == NULL);
2707
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002708 elf = kmod_module_get_elf(mod);
2709 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002710 return -errno;
2711
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002712 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002713 if (count < 0)
2714 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002715
2716 for (i = 0; i < count; i++) {
2717 struct kmod_module_dependency_symbol *mv;
2718 struct kmod_list *n;
2719
2720 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2721 symbols[i].bind,
2722 symbols[i].symbol);
2723 if (mv == NULL) {
2724 ret = -errno;
2725 kmod_module_dependency_symbols_free_list(*list);
2726 *list = NULL;
2727 goto list_error;
2728 }
2729
2730 n = kmod_list_append(*list, mv);
2731 if (n != NULL)
2732 *list = n;
2733 else {
2734 kmod_module_dependency_symbol_free(mv);
2735 kmod_module_dependency_symbols_free_list(*list);
2736 *list = NULL;
2737 ret = -ENOMEM;
2738 goto list_error;
2739 }
2740 }
2741 ret = count;
2742
2743list_error:
2744 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002745 return ret;
2746}
2747
2748/**
2749 * kmod_module_dependency_symbol_get_symbol:
2750 * @entry: a list entry representing a kmod module dependency_symbols
2751 *
2752 * Get the dependency symbol of a kmod module
2753 *
2754 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2755 * on failure. The string is owned by the dependency_symbols, do not free it.
2756 */
2757KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2758{
2759 struct kmod_module_dependency_symbol *dependency_symbol;
2760
2761 if (entry == NULL)
2762 return NULL;
2763
2764 dependency_symbol = entry->data;
2765 return dependency_symbol->symbol;
2766}
2767
2768/**
2769 * kmod_module_dependency_symbol_get_crc:
2770 * @entry: a list entry representing a kmod module dependency_symbol
2771 *
2772 * Get the crc of a kmod module dependency_symbol.
2773 *
2774 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2775 * failure. The string is owned by the dependency_symbol, do not free it.
2776 */
2777KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2778{
2779 struct kmod_module_dependency_symbol *dependency_symbol;
2780
2781 if (entry == NULL)
2782 return 0;
2783
2784 dependency_symbol = entry->data;
2785 return dependency_symbol->crc;
2786}
2787
2788/**
2789 * kmod_module_dependency_symbol_get_bind:
2790 * @entry: a list entry representing a kmod module dependency_symbol
2791 *
2792 * Get the bind type of a kmod module dependency_symbol.
2793 *
2794 * Returns: the bind of this kmod module dependency_symbol on success
2795 * or < 0 on failure.
2796 */
2797KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2798{
2799 struct kmod_module_dependency_symbol *dependency_symbol;
2800
2801 if (entry == NULL)
2802 return 0;
2803
2804 dependency_symbol = entry->data;
2805 return dependency_symbol->bind;
2806}
2807
2808/**
2809 * kmod_module_dependency_symbols_free_list:
2810 * @list: kmod module dependency_symbols list
2811 *
2812 * Release the resources taken by @list
2813 */
2814KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2815{
2816 while (list) {
2817 kmod_module_dependency_symbol_free(list->data);
2818 list = kmod_list_remove(list);
2819 }
2820}