blob: 1d90f3438623082491ce4bd362de7cb91059fbaa [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Lucas De Marchi8f788d52011-11-25 01:22:56 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi8f788d52011-11-25 01:22:56 -020010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Lucas De Marchi7636e722011-12-01 17:56:03 -020021#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020022#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020031#include <limits.h>
32#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/mman.h>
Kees Cook144d1822013-02-18 12:02:32 -080036#include <sys/syscall.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020037#include <sys/wait.h>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020038#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020039
Kees Cook144d1822013-02-18 12:02:32 -080040#ifdef HAVE_LINUX_MODULE_H
41#include <linux/module.h>
42#endif
43
Lucas De Marchi8f788d52011-11-25 01:22:56 -020044#include "libkmod.h"
Lucas De Marchi83b855a2013-07-04 16:13:11 -030045#include "libkmod-internal.h"
Lucas De Marchi8f788d52011-11-25 01:22:56 -020046
47/**
Lucas De Marchi66819512012-01-09 04:47:40 -020048 * SECTION:libkmod-module
49 * @short_description: operate on kernel modules
50 */
51
52/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020053 * kmod_module:
54 *
55 * Opaque object representing a module.
56 */
57struct kmod_module {
58 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020059 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020060 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020061 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020062 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020063 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020064 const char *install_commands; /* owned by kmod_config */
65 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020066 char *alias; /* only set if this module was created from an alias */
Lucas De Marchi1eff9422012-10-18 01:36:33 -030067 struct kmod_file *file;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020068 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020069 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020070 struct {
71 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020072 bool options : 1;
73 bool install_commands : 1;
74 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020075 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020076
77 /*
78 * private field used by kmod_module_get_probe_list() to detect
79 * dependency loops
80 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020081 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020082
83 /*
84 * set by kmod_module_get_probe_list: indicates for probe_insert()
85 * whether the module's command and softdep should be ignored
86 */
87 bool ignorecmd : 1;
Lucas De Marchi38052742012-02-16 20:43:16 -020088
89 /*
Michal Marek450bd1b2014-03-31 15:18:50 +020090 * set by kmod_module_get_probe_list: indicates whether this is the
91 * module the user asked for or its dependency, or whether this
92 * is a softdep only
93 */
94 bool required : 1;
95
96 /*
Lucas De Marchi38052742012-02-16 20:43:16 -020097 * if module was created by searching the modules.builtin file, this
98 * is set. There's nothing much useful one can do with such a
99 * "module", except knowing it's builtin.
100 */
101 bool builtin : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200102};
103
Lucas De Marchic35347f2011-12-12 10:48:02 -0200104static inline const char *path_join(const char *path, size_t prefixlen,
105 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200106{
107 size_t pathlen;
108
109 if (path[0] == '/')
110 return path;
111
112 pathlen = strlen(path);
113 if (prefixlen + pathlen + 1 >= PATH_MAX)
114 return NULL;
115
116 memcpy(buf + prefixlen, path, pathlen + 1);
117 return buf;
118}
119
Dave Reisneraf9572c2012-02-02 11:07:33 -0500120static inline bool module_is_inkernel(struct kmod_module *mod)
121{
122 int state = kmod_module_get_initstate(mod);
Lucas De Marchi38052742012-02-16 20:43:16 -0200123
Dave Reisneraf9572c2012-02-02 11:07:33 -0500124 if (state == KMOD_MODULE_LIVE ||
Dave Reisneraf9572c2012-02-02 11:07:33 -0500125 state == KMOD_MODULE_BUILTIN)
126 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200127
128 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500129}
130
Lucas De Marchi671d4892011-12-05 20:23:05 -0200131int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200132{
133 struct kmod_ctx *ctx = mod->ctx;
134 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200135 const char *dirname;
136 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200137 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200138 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200139 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200140
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200141 if (mod->init.dep)
142 return mod->n_dep;
143 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200144 mod->init.dep = true;
145
146 p = strchr(line, ':');
147 if (p == NULL)
148 return 0;
149
Lucas De Marchi671d4892011-12-05 20:23:05 -0200150 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200151 dirname = kmod_get_dirname(mod->ctx);
152 dirnamelen = strlen(dirname);
153 if (dirnamelen + 2 >= PATH_MAX)
154 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200155
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200156 memcpy(buf, dirname, dirnamelen);
157 buf[dirnamelen] = '/';
158 dirnamelen++;
159 buf[dirnamelen] = '\0';
160
161 if (mod->path == NULL) {
162 const char *str = path_join(line, dirnamelen, buf);
163 if (str == NULL)
164 return 0;
165 mod->path = strdup(str);
166 if (mod->path == NULL)
167 return 0;
168 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200169
Lucas De Marchi7636e722011-12-01 17:56:03 -0200170 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200171 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
172 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200173 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200174 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200175
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200176 path = path_join(p, dirnamelen, buf);
177 if (path == NULL) {
178 ERR(ctx, "could not join path '%s' and '%s'.\n",
179 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200180 goto fail;
181 }
182
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200183 err = kmod_module_new_from_path(ctx, path, &depmod);
184 if (err < 0) {
185 ERR(ctx, "ctx=%p path=%s error=%s\n",
186 ctx, path, strerror(-err));
187 goto fail;
188 }
189
190 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200191
Lucas De Marchib94a7372011-12-26 20:10:49 -0200192 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200193 n++;
194 }
195
196 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
197
198 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200199 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200200 return n;
201
202fail:
203 kmod_module_unref_list(list);
204 mod->init.dep = false;
205 return err;
206}
207
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200208void kmod_module_set_visited(struct kmod_module *mod, bool visited)
209{
210 mod->visited = visited;
211}
212
Lucas De Marchi38052742012-02-16 20:43:16 -0200213void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
214{
215 mod->builtin = builtin;
216}
217
Michal Marek450bd1b2014-03-31 15:18:50 +0200218void kmod_module_set_required(struct kmod_module *mod, bool required)
219{
220 mod->required = required;
221}
222
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200223/*
224 * Memory layout with alias:
225 *
226 * struct kmod_module {
227 * hashkey -----.
228 * alias -----. |
229 * name ----. | |
230 * } | | |
231 * name <----------' | |
232 * alias <-----------' |
233 * name\alias <--------'
234 *
235 * Memory layout without alias:
236 *
237 * struct kmod_module {
238 * hashkey ---.
239 * alias -----|----> NULL
240 * name ----. |
241 * } | |
242 * name <----------'-'
243 *
244 * @key is "name\alias" or "name" (in which case alias == NULL)
245 */
246static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
247 const char *name, size_t namelen,
248 const char *alias, size_t aliaslen,
249 struct kmod_module **mod)
250{
251 struct kmod_module *m;
252 size_t keylen;
253
254 m = kmod_pool_get_module(ctx, key);
255 if (m != NULL) {
256 *mod = kmod_module_ref(m);
257 return 0;
258 }
259
260 if (alias == NULL)
261 keylen = namelen;
262 else
263 keylen = namelen + aliaslen + 1;
264
265 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
Lucas De Marchi9f025612013-11-18 11:52:53 -0200266 if (m == NULL)
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200267 return -ENOMEM;
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200268
269 memset(m, 0, sizeof(*m));
270
271 m->ctx = kmod_ref(ctx);
272 m->name = (char *)m + sizeof(*m);
273 memcpy(m->name, key, keylen + 1);
274 if (alias == NULL) {
275 m->hashkey = m->name;
276 m->alias = NULL;
277 } else {
278 m->name[namelen] = '\0';
279 m->alias = m->name + namelen + 1;
280 m->hashkey = m->name + keylen + 1;
281 memcpy(m->hashkey, key, keylen + 1);
282 }
283
284 m->refcount = 1;
285 kmod_pool_add_module(ctx, m, m->hashkey);
286 *mod = m;
287
288 return 0;
289}
290
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200291/**
292 * kmod_module_new_from_name:
293 * @ctx: kmod library context
294 * @name: name of the module
295 * @mod: where to save the created struct kmod_module
296 *
297 * Create a new struct kmod_module using the module name. @name can not be an
298 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600299 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200300 *
301 * This function is also used internally by many others that return a new
302 * struct kmod_module or a new list of modules.
303 *
304 * The initial refcount is 1, and needs to be decremented to release the
305 * resources of the kmod_module. Since libkmod keeps track of all
306 * kmod_modules created, they are all released upon @ctx destruction too. Do
307 * not unref @ctx before all the desired operations with the returned
308 * kmod_module are done.
309 *
310 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
311 * module name or if memory allocation failed.
312 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200313KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
314 const char *name,
315 struct kmod_module **mod)
316{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200317 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200318 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200319
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200320 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200321 return -ENOENT;
322
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200323 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200324
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200325 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200326}
327
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200328int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
329 const char *name, struct kmod_module **mod)
330{
331 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200332 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200333 size_t namelen = strlen(name);
334 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200335
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200336 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200337 return -ENAMETOOLONG;
338
339 memcpy(key, name, namelen);
340 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200341 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200342
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200343 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200344 if (err < 0)
345 return err;
346
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200347 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200348}
349
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200350/**
351 * kmod_module_new_from_path:
352 * @ctx: kmod library context
353 * @path: path where to find the given module
354 * @mod: where to save the created struct kmod_module
355 *
356 * Create a new struct kmod_module using the module path. @path must be an
357 * existent file with in the filesystem and must be accessible to libkmod.
358 *
359 * The initial refcount is 1, and needs to be decremented to release the
360 * resources of the kmod_module. Since libkmod keeps track of all
361 * kmod_modules created, they are all released upon @ctx destruction too. Do
362 * not unref @ctx before all the desired operations with the returned
363 * kmod_module are done.
364 *
365 * If @path is relative, it's treated as relative to the current working
366 * directory. Otherwise, give an absolute path.
367 *
368 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
369 * it's not a valid file for a kmod_module or if memory allocation failed.
370 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200371KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
372 const char *path,
373 struct kmod_module **mod)
374{
375 struct kmod_module *m;
376 int err;
377 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200378 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200379 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200380 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200381
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200382 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200383 return -ENOENT;
384
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200385 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200386 if (abspath == NULL) {
387 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200388 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200389 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200390
391 err = stat(abspath, &st);
392 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200393 err = -errno;
394 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200395 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200396 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200397 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200398
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200399 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200400 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200401 free(abspath);
402 return -ENOENT;
403 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200404
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200405 m = kmod_pool_get_module(ctx, name);
406 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200407 if (m->path == NULL)
408 m->path = abspath;
409 else if (streq(m->path, abspath))
410 free(abspath);
411 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200412 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 -0200413 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200414 free(abspath);
415 return -EEXIST;
416 }
417
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200418 *mod = kmod_module_ref(m);
419 return 0;
420 }
421
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200422 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
423 if (err < 0)
424 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200425
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200426 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200427 *mod = m;
428
429 return 0;
430}
431
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200432/**
433 * kmod_module_unref:
434 * @mod: kmod module
435 *
436 * Drop a reference of the kmod module. If the refcount reaches zero, its
437 * resources are released.
438 *
439 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
440 * returns the passed @mod with its refcount decremented.
441 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200442KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
443{
444 if (mod == NULL)
445 return NULL;
446
447 if (--mod->refcount > 0)
448 return mod;
449
450 DBG(mod->ctx, "kmod_module %p released\n", mod);
451
Lucas De Marchi4084c172011-12-15 13:43:22 -0200452 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200453 kmod_module_unref_list(mod->dep);
Lucas De Marchi1eff9422012-10-18 01:36:33 -0300454
455 if (mod->file)
456 kmod_file_unref(mod->file);
457
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200458 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200459 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200460 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200461 free(mod);
462 return NULL;
463}
464
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200465/**
466 * kmod_module_ref:
467 * @mod: kmod module
468 *
469 * Take a reference of the kmod module, incrementing its refcount.
470 *
471 * Returns: the passed @module with its refcount incremented.
472 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200473KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
474{
475 if (mod == NULL)
476 return NULL;
477
478 mod->refcount++;
479
480 return mod;
481}
482
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200483#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
484 do { \
485 if ((_err) < 0) \
486 goto _label_err; \
487 if (*(_list) != NULL) \
488 goto finish; \
489 } while (0)
490
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200491/**
492 * kmod_module_new_from_lookup:
493 * @ctx: kmod library context
494 * @given_alias: alias to look for
495 * @list: an empty list where to save the list of modules matching
496 * @given_alias
497 *
498 * Create a new list of kmod modules using an alias or module name and lookup
499 * libkmod's configuration files and indexes in order to find the module.
500 * Once it's found in one of the places, it stops searching and create the
501 * list of modules that is saved in @list.
502 *
503 * The search order is: 1. aliases in configuration file; 2. module names in
504 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
505 * in modules.alias index.
506 *
507 * The initial refcount is 1, and needs to be decremented to release the
508 * resources of the kmod_module. The returned @list must be released by
509 * calling kmod_module_unref_list(). Since libkmod keeps track of all
510 * kmod_modules created, they are all released upon @ctx destruction too. Do
511 * not unref @ctx before all the desired operations with the returned list are
512 * completed.
513 *
514 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
515 * methods failed, which is basically due to memory allocation fail. If module
516 * is not found, it still returns 0, but @list is an empty list.
517 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200518KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200519 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200520 struct kmod_list **list)
521{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200522 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200523 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200524
Lucas De Marchi4308b172011-12-13 10:26:04 -0200525 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200526 return -ENOENT;
527
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200528 if (list == NULL || *list != NULL) {
529 ERR(ctx, "An empty list is needed to create lookup\n");
530 return -ENOSYS;
531 }
532
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200533 if (alias_normalize(given_alias, alias, NULL) < 0) {
534 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200535 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200536 }
537
538 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200539
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200540 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200541 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200542 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200543
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200544 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200545 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
546 CHECK_ERR_AND_FINISH(err, fail, list, finish);
547
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200548 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200549 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
550 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200551
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200552 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200553 err = kmod_lookup_alias_from_commands(ctx, alias, list);
554 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200555
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200556 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200557 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
558 CHECK_ERR_AND_FINISH(err, fail, list, finish);
559
Lucas De Marchi38052742012-02-16 20:43:16 -0200560 DBG(ctx, "lookup modules.builtin %s\n", alias);
561 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
562 CHECK_ERR_AND_FINISH(err, fail, list, finish);
563
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200564finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200565 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200566 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200567fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200568 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200569 kmod_module_unref_list(*list);
570 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200571 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200572}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200573#undef CHECK_ERR_AND_FINISH
574
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200575/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200576 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200577 * @list: list of kmod modules
578 *
579 * Drop a reference of each kmod module in @list and releases the resources
580 * taken by the list itself.
581 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800582 * Returns: 0
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200583 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200584KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
585{
586 for (; list != NULL; list = kmod_list_remove(list))
587 kmod_module_unref(list->data);
588
589 return 0;
590}
591
Lucas De Marchi0d467432011-12-31 11:15:52 -0200592/**
593 * kmod_module_get_filtered_blacklist:
594 * @ctx: kmod library context
595 * @input: list of kmod_module to be filtered with blacklist
596 * @output: where to save the new list
597 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200598 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500599 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200600 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500601 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200602 *
603 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
604 * list.
605 */
606KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
607 const struct kmod_list *input,
608 struct kmod_list **output)
609{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500610 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200611}
612
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200613static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
614{
615 if (!mod->init.dep) {
616 /* lazy init */
617 char *line = kmod_search_moddep(mod->ctx, mod->name);
618
619 if (line == NULL)
620 return NULL;
621
622 kmod_module_parse_depline((struct kmod_module *)mod, line);
623 free(line);
624
625 if (!mod->init.dep)
626 return NULL;
627 }
628
629 return mod->dep;
630}
631
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200632/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200633 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200634 * @mod: kmod module
635 *
636 * Search the modules.dep index to find the dependencies of the given @mod.
637 * The result is cached in @mod, so subsequent calls to this function will
638 * return the already searched list of modules.
639 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800640 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
641 * that can be released by calling kmod_module_unref_list().
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200642 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200643KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200644{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200645 struct kmod_list *l, *l_new, *list_new = NULL;
646
647 if (mod == NULL)
648 return NULL;
649
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200650 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200651
652 kmod_list_foreach(l, mod->dep) {
653 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
654 if (l_new == NULL) {
655 kmod_module_unref(l->data);
656 goto fail;
657 }
658
659 list_new = l_new;
660 }
661
662 return list_new;
663
664fail:
665 ERR(mod->ctx, "out of memory\n");
666 kmod_module_unref_list(list_new);
667 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200668}
669
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200670/**
671 * kmod_module_get_module:
672 * @entry: an entry in a list of kmod modules.
673 *
674 * Get the kmod module of this @entry in the list, increasing its refcount.
675 * After it's used, unref it. Since the refcount is incremented upon return,
676 * you still have to call kmod_module_unref_list() to release the list of kmod
677 * modules.
678 *
679 * Returns: NULL on failure or the kmod_module contained in this list entry
680 * with its refcount incremented.
681 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200682KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200683{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200684 if (entry == NULL)
685 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200686
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200687 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200688}
689
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200690/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200691 * kmod_module_get_name:
692 * @mod: kmod module
693 *
694 * Get the name of this kmod module. Name is always available, independently
695 * if it was created by kmod_module_new_from_name() or another function and
696 * it's always normalized (dashes are replaced with underscores).
697 *
698 * Returns: the name of this kmod module.
699 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200700KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200701{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200702 if (mod == NULL)
703 return NULL;
704
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200705 return mod->name;
706}
707
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200708/**
709 * kmod_module_get_path:
710 * @mod: kmod module
711 *
712 * Get the path of this kmod module. If this kmod module was not created by
713 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200714 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200715 *
716 * Returns: the path of this kmod module or NULL if such information is not
717 * available.
718 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200719KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200720{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200721 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200722
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200723 if (mod == NULL)
724 return NULL;
725
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200726 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200727
Lucas De Marchie005fac2011-12-08 10:42:34 -0200728 if (mod->path != NULL)
729 return mod->path;
730 if (mod->init.dep)
731 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200732
Lucas De Marchie005fac2011-12-08 10:42:34 -0200733 /* lazy init */
734 line = kmod_search_moddep(mod->ctx, mod->name);
735 if (line == NULL)
736 return NULL;
737
738 kmod_module_parse_depline((struct kmod_module *) mod, line);
739 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200740
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200741 return mod->path;
742}
743
744
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200745extern long delete_module(const char *name, unsigned int flags);
746
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200747/**
748 * kmod_module_remove_module:
749 * @mod: kmod module
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500750 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
Chengwei Yangd7152f62013-05-04 17:07:03 +0800751 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
752 * use by a kernel subsystem or other process;
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500753 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
754 * delete_module(2).
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200755 *
756 * Remove a module from Linux kernel.
757 *
758 * Returns: 0 on success or < 0 on failure.
759 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200760KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
761 unsigned int flags)
762{
763 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200764
765 if (mod == NULL)
766 return -ENOENT;
767
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500768 /* Filter out other flags and force ONONBLOCK */
769 flags &= KMOD_REMOVE_FORCE;
770 flags |= KMOD_REMOVE_NOWAIT;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200771
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200772 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200773 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200774 err = -errno;
775 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200776 }
777
Lucas De Marchiba998b92012-01-11 00:08:14 -0200778 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200779}
780
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200781extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200782
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200783/**
784 * kmod_module_insert_module:
785 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200786 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +0800787 * behavior of this function, valid flags are
788 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
789 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200790 * @options: module's options to pass to Linux Kernel.
791 *
792 * Insert a module in Linux kernel. It opens the file pointed by @mod,
793 * mmap'ing it and passing to kernel.
794 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200795 * Returns: 0 on success or < 0 on failure. If module is already loaded it
796 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200797 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200798KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200799 unsigned int flags,
800 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200801{
802 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200803 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200804 off_t size;
Michal Marekc2f4d852014-02-28 13:05:32 +0100805 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200806 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200807 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200808
809 if (mod == NULL)
810 return -ENOENT;
811
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200812 path = kmod_module_get_path(mod);
813 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500814 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200815 return -ENOSYS;
816 }
817
Michal Marekc2f4d852014-02-28 13:05:32 +0100818 mod->file = kmod_file_open(mod->ctx, path);
819 if (mod->file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200820 err = -errno;
821 return err;
822 }
823
Michal Marekc2f4d852014-02-28 13:05:32 +0100824 if (kmod_file_get_direct(mod->file)) {
Kees Cook144d1822013-02-18 12:02:32 -0800825 unsigned int kernel_flags = 0;
826
Kees Cook144d1822013-02-18 12:02:32 -0800827 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
828 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
829 if (flags & KMOD_INSERT_FORCE_MODVERSION)
830 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
Kees Cook144d1822013-02-18 12:02:32 -0800831
Michal Marekc2f4d852014-02-28 13:05:32 +0100832 err = finit_module(kmod_file_get_fd(mod->file), args, kernel_flags);
Kees Cook144d1822013-02-18 12:02:32 -0800833 if (err == 0 || errno != ENOSYS)
834 goto init_finished;
835 }
836
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200837 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
Michal Marekc2f4d852014-02-28 13:05:32 +0100838 elf = kmod_file_get_elf(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200839 if (elf == NULL) {
840 err = -errno;
Michal Marekc2f4d852014-02-28 13:05:32 +0100841 return err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200842 }
843
844 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
845 err = kmod_elf_strip_section(elf, "__versions");
846 if (err < 0)
847 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
848 }
849
850 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
851 err = kmod_elf_strip_vermagic(elf);
852 if (err < 0)
853 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
854 }
855
856 mem = kmod_elf_get_memory(elf);
Michal Marekc2f4d852014-02-28 13:05:32 +0100857 } else {
858 mem = kmod_file_get_contents(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200859 }
Michal Marekc2f4d852014-02-28 13:05:32 +0100860 size = kmod_file_get_size(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200861
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200862 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800863init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200864 if (err < 0) {
865 err = -errno;
866 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
867 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200868 return err;
869}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200870
Lucas De Marchiddbda022011-12-27 11:40:10 -0200871static bool module_is_blacklisted(struct kmod_module *mod)
872{
873 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300874 const struct kmod_config *config = kmod_get_config(ctx);
875 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200876 const struct kmod_list *l;
877
878 kmod_list_foreach(l, bl) {
879 const char *modname = kmod_blacklist_get_modname(l);
880
881 if (streq(modname, mod->name))
882 return true;
883 }
884
885 return false;
886}
887
Dave Reisnerd80b1032012-02-24 10:05:11 -0500888/**
889 * kmod_module_apply_filter
890 * @ctx: kmod library context
Chengwei Yangd7152f62013-05-04 17:07:03 +0800891 * @filter_type: bitmask to filter modules out, valid types are
892 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
893 * KMOD_FILTER_BUILTIN: filter builtin modules out.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500894 * @input: list of kmod_module to be filtered
895 * @output: where to save the new list
896 *
897 * Given a list @input, this function filter it out by the filter mask
898 * and save it in @output.
899 *
900 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
901 * list.
902 */
903KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
904 enum kmod_filter filter_type,
905 const struct kmod_list *input,
906 struct kmod_list **output)
907{
908 const struct kmod_list *li;
909
910 if (ctx == NULL || output == NULL)
911 return -ENOENT;
912
913 *output = NULL;
914 if (input == NULL)
915 return 0;
916
917 kmod_list_foreach(li, input) {
918 struct kmod_module *mod = li->data;
919 struct kmod_list *node;
920
921 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
922 module_is_blacklisted(mod))
923 continue;
924
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500925 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500926 continue;
927
928 node = kmod_list_append(*output, mod);
929 if (node == NULL)
930 goto fail;
931
932 *output = node;
933 kmod_module_ref(mod);
934 }
935
936 return 0;
937
938fail:
939 kmod_module_unref_list(*output);
940 *output = NULL;
941 return -ENOMEM;
942}
943
Lucas De Marchiddbda022011-12-27 11:40:10 -0200944static int command_do(struct kmod_module *mod, const char *type,
945 const char *cmd)
946{
947 const char *modname = kmod_module_get_name(mod);
948 int err;
949
950 DBG(mod->ctx, "%s %s\n", type, cmd);
951
952 setenv("MODPROBE_MODULE", modname, 1);
953 err = system(cmd);
954 unsetenv("MODPROBE_MODULE");
955
956 if (err == -1 || WEXITSTATUS(err)) {
957 ERR(mod->ctx, "Error running %s command for %s\n",
958 type, modname);
959 if (err != -1)
960 err = -WEXITSTATUS(err);
961 }
962
963 return err;
964}
965
Lucas De Marchib1a51252012-01-29 01:49:09 -0200966struct probe_insert_cb {
967 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
968 void *data;
969};
970
Lucas De Marchiddbda022011-12-27 11:40:10 -0200971static int module_do_install_commands(struct kmod_module *mod,
972 const char *options,
973 struct probe_insert_cb *cb)
974{
975 const char *command = kmod_module_get_install_commands(mod);
Lucas De Marchi9f025612013-11-18 11:52:53 -0200976 char *p;
977 _cleanup_free_ char *cmd;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200978 int err;
979 size_t cmdlen, options_len, varlen;
980
981 assert(command);
982
983 if (options == NULL)
984 options = "";
985
986 options_len = strlen(options);
987 cmdlen = strlen(command);
988 varlen = sizeof("$CMDLINE_OPTS") - 1;
989
990 cmd = memdup(command, cmdlen + 1);
991 if (cmd == NULL)
992 return -ENOMEM;
993
994 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
995 size_t prefixlen = p - cmd;
996 size_t suffixlen = cmdlen - prefixlen - varlen;
997 size_t slen = cmdlen - varlen + options_len;
998 char *suffix = p + varlen;
999 char *s = malloc(slen + 1);
Lucas De Marchi9f025612013-11-18 11:52:53 -02001000 if (!s)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001001 return -ENOMEM;
Lucas De Marchi9f025612013-11-18 11:52:53 -02001002
Lucas De Marchiddbda022011-12-27 11:40:10 -02001003 memcpy(s, cmd, p - cmd);
1004 memcpy(s + prefixlen, options, options_len);
1005 memcpy(s + prefixlen + options_len, suffix, suffixlen);
1006 s[slen] = '\0';
1007
1008 free(cmd);
1009 cmd = s;
1010 cmdlen = slen;
1011 }
1012
1013 if (cb->run_install != NULL)
1014 err = cb->run_install(mod, cmd, cb->data);
1015 else
1016 err = command_do(mod, "install", cmd);
1017
Lucas De Marchiddbda022011-12-27 11:40:10 -02001018 return err;
1019}
1020
Lucas De Marchiddbda022011-12-27 11:40:10 -02001021static char *module_options_concat(const char *opt, const char *xopt)
1022{
1023 // TODO: we might need to check if xopt overrides options on opt
1024 size_t optlen = opt == NULL ? 0 : strlen(opt);
1025 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1026 char *r;
1027
1028 if (optlen == 0 && xoptlen == 0)
1029 return NULL;
1030
1031 r = malloc(optlen + xoptlen + 2);
1032
1033 if (opt != NULL) {
1034 memcpy(r, opt, optlen);
1035 r[optlen] = ' ';
1036 optlen++;
1037 }
1038
1039 if (xopt != NULL)
1040 memcpy(r + optlen, xopt, xoptlen);
1041
1042 r[optlen + xoptlen] = '\0';
1043
1044 return r;
1045}
1046
Lucas De Marchib1a51252012-01-29 01:49:09 -02001047static int __kmod_module_get_probe_list(struct kmod_module *mod,
Michal Marek450bd1b2014-03-31 15:18:50 +02001048 bool required,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001049 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001050 struct kmod_list **list);
1051
1052/* re-entrant */
1053static int __kmod_module_fill_softdep(struct kmod_module *mod,
1054 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001055{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001056 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001057 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001058
1059 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001060 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001061 ERR(mod->ctx, "could not get softdep: %s\n",
1062 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001063 goto fail;
1064 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001065
Lucas De Marchib1a51252012-01-29 01:49:09 -02001066 kmod_list_foreach(l, pre) {
1067 struct kmod_module *m = l->data;
Michal Marek450bd1b2014-03-31 15:18:50 +02001068 err = __kmod_module_get_probe_list(m, false, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001069 if (err < 0)
1070 goto fail;
1071 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001072
Lucas De Marchib1a51252012-01-29 01:49:09 -02001073 l = kmod_list_append(*list, kmod_module_ref(mod));
1074 if (l == NULL) {
1075 kmod_module_unref(mod);
1076 err = -ENOMEM;
1077 goto fail;
1078 }
1079 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001080 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001081
Lucas De Marchib1a51252012-01-29 01:49:09 -02001082 kmod_list_foreach(l, post) {
1083 struct kmod_module *m = l->data;
Michal Marek450bd1b2014-03-31 15:18:50 +02001084 err = __kmod_module_get_probe_list(m, false, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001085 if (err < 0)
1086 goto fail;
1087 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001088
Lucas De Marchib1a51252012-01-29 01:49:09 -02001089fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001090 kmod_module_unref_list(pre);
1091 kmod_module_unref_list(post);
1092
1093 return err;
1094}
1095
Lucas De Marchib1a51252012-01-29 01:49:09 -02001096/* re-entrant */
1097static int __kmod_module_get_probe_list(struct kmod_module *mod,
Michal Marek450bd1b2014-03-31 15:18:50 +02001098 bool required,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001099 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001100 struct kmod_list **list)
1101{
1102 struct kmod_list *dep, *l;
1103 int err = 0;
1104
1105 if (mod->visited) {
1106 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1107 mod->name);
1108 return 0;
1109 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001110 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001111
1112 dep = kmod_module_get_dependencies(mod);
Michal Marek450bd1b2014-03-31 15:18:50 +02001113 if (required) {
1114 /*
1115 * Called from kmod_module_probe_insert_module(); set the
1116 * ->required flag on mod and all its dependencies before
1117 * they are possibly visited through some softdeps.
1118 */
1119 mod->required = true;
1120 kmod_list_foreach(l, dep) {
1121 struct kmod_module *m = l->data;
1122 m->required = true;
1123 }
1124 }
1125
Lucas De Marchib1a51252012-01-29 01:49:09 -02001126 kmod_list_foreach(l, dep) {
1127 struct kmod_module *m = l->data;
1128 err = __kmod_module_fill_softdep(m, list);
1129 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001130 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001131 }
1132
Lucas De Marchi89e92482012-01-29 02:35:46 -02001133 if (ignorecmd) {
1134 l = kmod_list_append(*list, kmod_module_ref(mod));
1135 if (l == NULL) {
1136 kmod_module_unref(mod);
1137 err = -ENOMEM;
1138 goto finish;
1139 }
1140 *list = l;
1141 mod->ignorecmd = true;
1142 } else
1143 err = __kmod_module_fill_softdep(mod, list);
1144
Lucas De Marchib1a51252012-01-29 01:49:09 -02001145finish:
1146 kmod_module_unref_list(dep);
1147 return err;
1148}
1149
1150static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001151 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001152 struct kmod_list **list)
1153{
1154 int err;
1155
1156 assert(mod != NULL);
1157 assert(list != NULL && *list == NULL);
1158
1159 /*
1160 * Make sure we don't get screwed by previous calls to this function
1161 */
1162 kmod_set_modules_visited(mod->ctx, false);
Michal Marek450bd1b2014-03-31 15:18:50 +02001163 kmod_set_modules_required(mod->ctx, false);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001164
Michal Marek450bd1b2014-03-31 15:18:50 +02001165 err = __kmod_module_get_probe_list(mod, true, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001166 if (err < 0) {
1167 kmod_module_unref_list(*list);
1168 *list = NULL;
1169 }
1170
1171 return err;
1172}
1173
Lucas De Marchiddbda022011-12-27 11:40:10 -02001174/**
1175 * kmod_module_probe_insert_module:
1176 * @mod: kmod module
1177 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +08001178 * behavior of this function, valid flags are
1179 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1180 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1181 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1182 * commands and softdeps configured in the system;
1183 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1184 * live in kernel or not;
1185 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1186 * associated callback function;
1187 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1188 * and the module is already live in kernel, the function will fail if this
1189 * flag is specified;
1190 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1191 * filter to this module and its dependencies. If any of the dependencies (or
1192 * the module) is blacklisted, the probe will fail, unless the blacklisted
1193 * module is already live in kernel;
1194 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1195 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1196 * alias and is blacklisted.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001197 * @extra_options: module's options to pass to Linux Kernel. It applies only
1198 * to @mod, not to its dependencies.
1199 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001200 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001201 * @print_action: function to call with the action being taken (install or
1202 * insmod). It's useful for tools like modprobe when running with verbose
1203 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001204 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001205 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001206 * install commands and applying blacklist.
1207 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001208 * If @run_install is NULL, this function will fork and exec by calling
1209 * system(3). Don't pass a NULL argument in @run_install if your binary is
1210 * setuid/setgid (see warning in system(3)). If you need control over the
1211 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001212 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001213 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1214 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001215 */
1216KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1217 unsigned int flags, const char *extra_options,
1218 int (*run_install)(struct kmod_module *m,
1219 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001220 const void *data,
1221 void (*print_action)(struct kmod_module *m,
1222 bool install,
1223 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001224{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001225 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001226 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001227 int err;
1228
1229 if (mod == NULL)
1230 return -ENOENT;
1231
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001232 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1233 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001234 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001235 return -EEXIST;
1236 else
1237 return 0;
1238 }
1239
Lucas De Marchi68820172012-08-17 09:38:05 -03001240 /*
1241 * Ugly assignement + check. We need to check if we were told to check
1242 * blacklist and also return the reason why we failed.
1243 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1244 * module is an alias, so we also need to check it
1245 */
1246 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1247 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1248 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001249 if (module_is_blacklisted(mod))
1250 return err;
1251 }
1252
Lucas De Marchi89e92482012-01-29 02:35:46 -02001253 err = kmod_module_get_probe_list(mod,
1254 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001255 if (err < 0)
1256 return err;
1257
1258 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1259 struct kmod_list *filtered = NULL;
1260
Dave Reisnerd80b1032012-02-24 10:05:11 -05001261 err = kmod_module_apply_filter(mod->ctx,
1262 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001263 if (err < 0)
1264 return err;
1265
1266 kmod_module_unref_list(list);
1267 if (filtered == NULL)
1268 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1269
1270 list = filtered;
1271 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001272
1273 cb.run_install = run_install;
1274 cb.data = (void *) data;
1275
Lucas De Marchib1a51252012-01-29 01:49:09 -02001276 kmod_list_foreach(l, list) {
1277 struct kmod_module *m = l->data;
1278 const char *moptions = kmod_module_get_options(m);
1279 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001280 char *options;
1281
1282 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1283 && module_is_inkernel(m)) {
1284 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1285 m->name);
1286 err = -EEXIST;
1287 goto finish_module;
1288 }
1289
1290 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001291 m == mod ? extra_options : NULL);
1292
Lucas De Marchi89e92482012-01-29 02:35:46 -02001293 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001294 if (print_action != NULL)
1295 print_action(m, true, options ?: "");
1296
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001297 if (!(flags & KMOD_PROBE_DRY_RUN))
1298 err = module_do_install_commands(m, options,
1299 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001300 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001301 if (print_action != NULL)
1302 print_action(m, false, options ?: "");
1303
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001304 if (!(flags & KMOD_PROBE_DRY_RUN))
1305 err = kmod_module_insert_module(m, flags,
1306 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001307 }
1308
1309 free(options);
1310
Lucas De Marchiabd55572012-02-19 04:20:30 -02001311finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001312 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001313 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001314 * already loaded and the module being loaded is not a softdep
1315 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001316 *
1317 * We need to check here because of race conditions. We
1318 * checked first if module was already loaded but it may have
1319 * been loaded between the check and the moment we try to
1320 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001321 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001322 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001323 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001324 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001325
Michal Marek450bd1b2014-03-31 15:18:50 +02001326 /*
1327 * Ignore errors from softdeps
1328 */
1329 if (err == -EEXIST || !m->required)
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001330 err = 0;
Michal Marek450bd1b2014-03-31 15:18:50 +02001331
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001332 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001333 break;
1334 }
1335
1336 kmod_module_unref_list(list);
1337 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001338}
1339
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001340/**
1341 * kmod_module_get_options:
1342 * @mod: kmod module
1343 *
1344 * Get options of this kmod module. Options come from the configuration file
1345 * and are cached in @mod. The first call to this function will search for
1346 * this module in configuration and subsequent calls return the cached string.
1347 *
1348 * Returns: a string with all the options separated by spaces. This string is
1349 * owned by @mod, do not free it.
1350 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001351KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1352{
1353 if (mod == NULL)
1354 return NULL;
1355
1356 if (!mod->init.options) {
1357 /* lazy init */
1358 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001359 const struct kmod_list *l;
1360 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001361 char *opts = NULL;
1362 size_t optslen = 0;
1363
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001364 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001365
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001366 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001367 const char *modname = kmod_option_get_modname(l);
1368 const char *str;
1369 size_t len;
1370 void *tmp;
1371
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001372 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1373 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1374 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001375 continue;
1376
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001377 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 -02001378 str = kmod_option_get_options(l);
1379 len = strlen(str);
1380 if (len < 1)
1381 continue;
1382
1383 tmp = realloc(opts, optslen + len + 2);
1384 if (tmp == NULL) {
1385 free(opts);
1386 goto failed;
1387 }
1388
1389 opts = tmp;
1390
1391 if (optslen > 0) {
1392 opts[optslen] = ' ';
1393 optslen++;
1394 }
1395
1396 memcpy(opts + optslen, str, len);
1397 optslen += len;
1398 opts[optslen] = '\0';
1399 }
1400
1401 m->init.options = true;
1402 m->options = opts;
1403 }
1404
1405 return mod->options;
1406
1407failed:
1408 ERR(mod->ctx, "out of memory\n");
1409 return NULL;
1410}
1411
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001412/**
1413 * kmod_module_get_install_commands:
1414 * @mod: kmod module
1415 *
1416 * Get install commands for this kmod module. Install commands come from the
1417 * configuration file and are cached in @mod. The first call to this function
1418 * will search for this module in configuration and subsequent calls return
1419 * the cached string. The install commands are returned as they were in the
1420 * configuration, concatenated by ';'. No other processing is made in this
1421 * string.
1422 *
1423 * Returns: a string with all install commands separated by semicolons. This
1424 * string is owned by @mod, do not free it.
1425 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001426KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1427{
1428 if (mod == NULL)
1429 return NULL;
1430
1431 if (!mod->init.install_commands) {
1432 /* lazy init */
1433 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001434 const struct kmod_list *l;
1435 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001436
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001437 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001438
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001439 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001440 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001441
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001442 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001443 continue;
1444
Lucas De Marchi60f67602011-12-16 03:33:26 -02001445 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001446
Lucas De Marchi60f67602011-12-16 03:33:26 -02001447 /*
1448 * find only the first command, as modprobe from
1449 * module-init-tools does
1450 */
1451 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001452 }
1453
1454 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001455 }
1456
1457 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001458}
1459
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001460void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1461{
1462 mod->init.install_commands = true;
1463 mod->install_commands = cmd;
1464}
1465
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001466static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1467{
1468 struct kmod_list *ret = NULL;
1469 unsigned i;
1470
1471 for (i = 0; i < count; i++) {
1472 const char *depname = array[i];
1473 struct kmod_list *lst = NULL;
1474 int err;
1475
1476 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1477 if (err < 0) {
1478 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1479 continue;
1480 } else if (lst != NULL)
1481 ret = kmod_list_append_list(ret, lst);
1482 }
1483 return ret;
1484}
1485
1486/**
1487 * kmod_module_get_softdeps:
1488 * @mod: kmod module
1489 * @pre: where to save the list of preceding soft dependencies.
1490 * @post: where to save the list of post soft dependencies.
1491 *
1492 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001493 * from configuration file and are not cached in @mod because it may include
1494 * dependency cycles that would make we leak kmod_module. Any call
1495 * to this function will search for this module in configuration, allocate a
1496 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001497 *
1498 * Both @pre and @post are newly created list of kmod_module and
1499 * should be unreferenced with kmod_module_unref_list().
1500 *
1501 * Returns: 0 on success or < 0 otherwise.
1502 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001503KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1504 struct kmod_list **pre,
1505 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001506{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001507 const struct kmod_list *l;
1508 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001509
1510 if (mod == NULL || pre == NULL || post == NULL)
1511 return -ENOENT;
1512
1513 assert(*pre == NULL);
1514 assert(*post == NULL);
1515
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001516 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001517
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001518 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001519 const char *modname = kmod_softdep_get_name(l);
1520 const char * const *array;
1521 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001522
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001523 if (fnmatch(modname, mod->name, 0) != 0)
1524 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001525
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001526 array = kmod_softdep_get_pre(l, &count);
1527 *pre = lookup_softdep(mod->ctx, array, count);
1528 array = kmod_softdep_get_post(l, &count);
1529 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001530
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001531 /*
1532 * find only the first command, as modprobe from
1533 * module-init-tools does
1534 */
1535 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001536 }
1537
1538 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001539}
1540
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001541/**
1542 * kmod_module_get_remove_commands:
1543 * @mod: kmod module
1544 *
1545 * Get remove commands for this kmod module. Remove commands come from the
1546 * configuration file and are cached in @mod. The first call to this function
1547 * will search for this module in configuration and subsequent calls return
1548 * the cached string. The remove commands are returned as they were in the
1549 * configuration, concatenated by ';'. No other processing is made in this
1550 * string.
1551 *
1552 * Returns: a string with all remove commands separated by semicolons. This
1553 * string is owned by @mod, do not free it.
1554 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001555KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1556{
1557 if (mod == NULL)
1558 return NULL;
1559
1560 if (!mod->init.remove_commands) {
1561 /* lazy init */
1562 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001563 const struct kmod_list *l;
1564 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001565
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001566 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001567
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001568 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001569 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001570
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001571 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001572 continue;
1573
Lucas De Marchi60f67602011-12-16 03:33:26 -02001574 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001575
Lucas De Marchi60f67602011-12-16 03:33:26 -02001576 /*
1577 * find only the first command, as modprobe from
1578 * module-init-tools does
1579 */
1580 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001581 }
1582
1583 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001584 }
1585
1586 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001587}
1588
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001589void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1590{
1591 mod->init.remove_commands = true;
1592 mod->remove_commands = cmd;
1593}
1594
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001595/**
1596 * SECTION:libkmod-loaded
1597 * @short_description: currently loaded modules
1598 *
1599 * Information about currently loaded modules, as reported by Linux kernel.
1600 * These information are not cached by libkmod and are always read from /sys
1601 * and /proc/modules.
1602 */
1603
1604/**
1605 * kmod_module_new_from_loaded:
1606 * @ctx: kmod library context
1607 * @list: where to save the list of loaded modules
1608 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001609 * Create a new list of kmod modules with all modules currently loaded in
1610 * kernel. It uses /proc/modules to get the names of loaded modules and to
1611 * create kmod modules by calling kmod_module_new_from_name() in each of them.
Chengwei Yang491c4902013-05-04 17:07:02 +08001612 * They are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001613 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001614 * The initial refcount is 1, and needs to be decremented to release the
1615 * resources of the kmod_module. The returned @list must be released by
1616 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1617 * kmod_modules created, they are all released upon @ctx destruction too. Do
1618 * not unref @ctx before all the desired operations with the returned list are
1619 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001620 *
1621 * Returns: 0 on success or < 0 on error.
1622 */
1623KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1624 struct kmod_list **list)
1625{
1626 struct kmod_list *l = NULL;
1627 FILE *fp;
1628 char line[4096];
1629
1630 if (ctx == NULL || list == NULL)
1631 return -ENOENT;
1632
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001633 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001634 if (fp == NULL) {
1635 int err = -errno;
1636 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1637 return err;
1638 }
1639
1640 while (fgets(line, sizeof(line), fp)) {
1641 struct kmod_module *m;
1642 struct kmod_list *node;
1643 int err;
1644 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1645
1646 err = kmod_module_new_from_name(ctx, name, &m);
1647 if (err < 0) {
1648 ERR(ctx, "could not get module from name '%s': %s\n",
1649 name, strerror(-err));
1650 continue;
1651 }
1652
1653 node = kmod_list_append(l, m);
1654 if (node)
1655 l = node;
1656 else {
1657 ERR(ctx, "out of memory\n");
1658 kmod_module_unref(m);
1659 }
1660 }
1661
1662 fclose(fp);
1663 *list = l;
1664
1665 return 0;
1666}
1667
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001668/**
1669 * kmod_module_initstate_str:
1670 * @state: the state as returned by kmod_module_get_initstate()
1671 *
1672 * Translate a initstate to a string.
1673 *
1674 * Returns: the string associated to the @state. This string is statically
1675 * allocated, do not free it.
1676 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001677KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1678{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001679 switch (state) {
1680 case KMOD_MODULE_BUILTIN:
1681 return "builtin";
1682 case KMOD_MODULE_LIVE:
1683 return "live";
1684 case KMOD_MODULE_COMING:
1685 return "coming";
1686 case KMOD_MODULE_GOING:
1687 return "going";
1688 default:
1689 return NULL;
1690 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001691}
1692
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001693/**
1694 * kmod_module_get_initstate:
1695 * @mod: kmod module
1696 *
1697 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1698 * /sys filesystem.
1699 *
Chengwei Yangd7152f62013-05-04 17:07:03 +08001700 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1701 * KMOD_MODULE_BUILTIN: module is builtin;
1702 * KMOD_MODULE_LIVE: module is live in kernel;
1703 * KMOD_MODULE_COMING: module is being loaded;
1704 * KMOD_MODULE_GOING: module is being unloaded.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001705 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001706KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1707{
1708 char path[PATH_MAX], buf[32];
1709 int fd, err, pathlen;
1710
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001711 if (mod == NULL)
1712 return -ENOENT;
1713
Lucas De Marchi38052742012-02-16 20:43:16 -02001714 if (mod->builtin)
1715 return KMOD_MODULE_BUILTIN;
1716
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001717 pathlen = snprintf(path, sizeof(path),
1718 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001719 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001720 if (fd < 0) {
1721 err = -errno;
1722
Lucas De Marchiddbda022011-12-27 11:40:10 -02001723 DBG(mod->ctx, "could not open '%s': %s\n",
1724 path, strerror(-err));
1725
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001726 if (pathlen > (int)sizeof("/initstate") - 1) {
1727 struct stat st;
1728 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1729 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1730 return KMOD_MODULE_BUILTIN;
1731 }
1732
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001733 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001734 path, strerror(-err));
1735 return err;
1736 }
1737
1738 err = read_str_safe(fd, buf, sizeof(buf));
1739 close(fd);
1740 if (err < 0) {
1741 ERR(mod->ctx, "could not read from '%s': %s\n",
1742 path, strerror(-err));
1743 return err;
1744 }
1745
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001746 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001747 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001748 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001749 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001750 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001751 return KMOD_MODULE_GOING;
1752
1753 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1754 return -EINVAL;
1755}
1756
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001757/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001758 * kmod_module_get_size:
1759 * @mod: kmod module
1760 *
Dave Reisner486f9012012-06-28 09:09:48 -04001761 * Get the size of this kmod module as returned by Linux kernel. If supported,
1762 * the size is read from the coresize attribute in /sys/module. For older
1763 * kernels, this falls back on /proc/modules and searches for the specified
1764 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001765 *
1766 * Returns: the size of this kmod module.
1767 */
1768KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1769{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001770 FILE *fp;
1771 char line[4096];
1772 int lineno = 0;
1773 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001774 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001775
1776 if (mod == NULL)
1777 return -ENOENT;
1778
Dave Reisner486f9012012-06-28 09:09:48 -04001779 /* try to open the module dir in /sys. If this fails, don't
1780 * bother trying to find the size as we know the module isn't
1781 * loaded.
1782 */
1783 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1784 dfd = open(line, O_RDONLY);
1785 if (dfd < 0)
1786 return -errno;
1787
1788 /* available as of linux 3.3.x */
1789 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1790 if (cfd >= 0) {
1791 if (read_str_long(cfd, &size, 10) < 0)
1792 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1793 close(cfd);
1794 goto done;
1795 }
1796
1797 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001798 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001799 if (fp == NULL) {
1800 int err = -errno;
1801 ERR(mod->ctx,
1802 "could not open /proc/modules: %s\n", strerror(errno));
1803 return err;
1804 }
1805
1806 while (fgets(line, sizeof(line), fp)) {
1807 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1808 long value;
1809
1810 lineno++;
1811 if (tok == NULL || !streq(tok, mod->name))
1812 continue;
1813
1814 tok = strtok_r(NULL, " \t", &saveptr);
1815 if (tok == NULL) {
1816 ERR(mod->ctx,
1817 "invalid line format at /proc/modules:%d\n", lineno);
1818 break;
1819 }
1820
1821 value = strtol(tok, &endptr, 10);
1822 if (endptr == tok || *endptr != '\0') {
1823 ERR(mod->ctx,
1824 "invalid line format at /proc/modules:%d\n", lineno);
1825 break;
1826 }
1827
1828 size = value;
1829 break;
1830 }
1831 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001832
1833done:
1834 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001835 return size;
1836}
1837
1838/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001839 * kmod_module_get_refcnt:
1840 * @mod: kmod module
1841 *
1842 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1843 * /sys filesystem.
1844 *
1845 * Returns: 0 on success or < 0 on failure.
1846 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001847KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1848{
1849 char path[PATH_MAX];
1850 long refcnt;
1851 int fd, err;
1852
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001853 if (mod == NULL)
1854 return -ENOENT;
1855
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001856 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001857 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001858 if (fd < 0) {
1859 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001860 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001861 path, strerror(errno));
1862 return err;
1863 }
1864
1865 err = read_str_long(fd, &refcnt, 10);
1866 close(fd);
1867 if (err < 0) {
1868 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1869 path, strerror(-err));
1870 return err;
1871 }
1872
1873 return (int)refcnt;
1874}
1875
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001876/**
1877 * kmod_module_get_holders:
1878 * @mod: kmod module
1879 *
1880 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1881 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1882 *
1883 * Returns: a new list of kmod modules on success or NULL on failure.
1884 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001885KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1886{
1887 char dname[PATH_MAX];
1888 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001889 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001890 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001891
Lucas De Marchib9a7da32013-04-23 20:47:28 -03001892 if (mod == NULL || mod->ctx == NULL)
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001893 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001894
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001895 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1896
1897 d = opendir(dname);
1898 if (d == NULL) {
1899 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001900 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001901 return NULL;
1902 }
1903
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001904 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001905 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001906 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001907 int err;
1908
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001909 if (dent->d_name[0] == '.') {
1910 if (dent->d_name[1] == '\0' ||
1911 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001912 continue;
1913 }
1914
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001915 err = kmod_module_new_from_name(mod->ctx, dent->d_name,
1916 &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001917 if (err < 0) {
1918 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001919 dent->d_name, strerror(-err));
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001920 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001921 }
1922
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001923 l = kmod_list_append(list, holder);
1924 if (l != NULL) {
1925 list = l;
1926 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001927 ERR(mod->ctx, "out of memory\n");
1928 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001929 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001930 }
1931 }
1932
1933 closedir(d);
1934 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001935
1936fail:
1937 closedir(d);
1938 kmod_module_unref_list(list);
1939 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001940}
1941
1942struct kmod_module_section {
1943 unsigned long address;
1944 char name[];
1945};
1946
1947static void kmod_module_section_free(struct kmod_module_section *section)
1948{
1949 free(section);
1950}
1951
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001952/**
1953 * kmod_module_get_sections:
1954 * @mod: kmod module
1955 *
1956 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1957 * structure contained in this list is internal to libkmod and their fields
1958 * can be obtained by calling kmod_module_section_get_name() and
1959 * kmod_module_section_get_address().
1960 *
1961 * After use, free the @list by calling kmod_module_section_free_list().
1962 *
1963 * Returns: a new list of kmod module sections on success or NULL on failure.
1964 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001965KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1966{
1967 char dname[PATH_MAX];
1968 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001969 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001970 DIR *d;
1971 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001972
1973 if (mod == NULL)
1974 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001975
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001976 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1977
1978 d = opendir(dname);
1979 if (d == NULL) {
1980 ERR(mod->ctx, "could not open '%s': %s\n",
1981 dname, strerror(errno));
1982 return NULL;
1983 }
1984
1985 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001986
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001987 for (dent = readdir(d); dent; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001988 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001989 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001990 unsigned long address;
1991 size_t namesz;
1992 int fd, err;
1993
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001994 if (dent->d_name[0] == '.') {
1995 if (dent->d_name[1] == '\0' ||
1996 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001997 continue;
1998 }
1999
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002000 fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002001 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002002 ERR(mod->ctx, "could not open '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002003 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002004 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002005 }
2006
2007 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002008 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002009 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002010 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002011 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002012 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002013 }
2014
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002015 namesz = strlen(dent->d_name) + 1;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002016 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002017
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002018 if (section == NULL) {
2019 ERR(mod->ctx, "out of memory\n");
2020 goto fail;
2021 }
2022
2023 section->address = address;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002024 memcpy(section->name, dent->d_name, namesz);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002025
2026 l = kmod_list_append(list, section);
2027 if (l != NULL) {
2028 list = l;
2029 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002030 ERR(mod->ctx, "out of memory\n");
2031 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002032 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002033 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002034 }
2035
2036 closedir(d);
2037 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002038
2039fail:
2040 closedir(d);
2041 kmod_module_unref_list(list);
2042 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002043}
2044
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002045/**
2046 * kmod_module_section_get_module_name:
2047 * @entry: a list entry representing a kmod module section
2048 *
2049 * Get the name of a kmod module section.
2050 *
2051 * After use, free the @list by calling kmod_module_section_free_list().
2052 *
2053 * Returns: the name of this kmod module section on success or NULL on
2054 * failure. The string is owned by the section, do not free it.
2055 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002056KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2057{
2058 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002059
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002060 if (entry == NULL)
2061 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002062
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002063 section = entry->data;
2064 return section->name;
2065}
2066
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002067/**
2068 * kmod_module_section_get_address:
2069 * @entry: a list entry representing a kmod module section
2070 *
2071 * Get the address of a kmod module section.
2072 *
2073 * After use, free the @list by calling kmod_module_section_free_list().
2074 *
2075 * Returns: the address of this kmod module section on success or ULONG_MAX
2076 * on failure.
2077 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002078KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2079{
2080 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002081
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002082 if (entry == NULL)
2083 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002084
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002085 section = entry->data;
2086 return section->address;
2087}
2088
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002089/**
2090 * kmod_module_section_free_list:
2091 * @list: kmod module section list
2092 *
2093 * Release the resources taken by @list
2094 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002095KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2096{
2097 while (list) {
2098 kmod_module_section_free(list->data);
2099 list = kmod_list_remove(list);
2100 }
2101}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002102
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002103static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2104{
2105 if (mod->file == NULL) {
2106 const char *path = kmod_module_get_path(mod);
2107
2108 if (path == NULL) {
2109 errno = ENOENT;
2110 return NULL;
2111 }
2112
2113 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2114 path);
2115 if (mod->file == NULL)
2116 return NULL;
2117 }
2118
2119 return kmod_file_get_elf(mod->file);
2120}
2121
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002122struct kmod_module_info {
2123 char *key;
2124 char value[];
2125};
2126
2127static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2128{
2129 struct kmod_module_info *info;
2130
2131 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2132 if (info == NULL)
2133 return NULL;
2134
2135 info->key = (char *)info + sizeof(struct kmod_module_info)
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002136 + valuelen + 1;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002137 memcpy(info->key, key, keylen);
2138 info->key[keylen] = '\0';
2139 memcpy(info->value, value, valuelen);
2140 info->value[valuelen] = '\0';
2141 return info;
2142}
2143
2144static void kmod_module_info_free(struct kmod_module_info *info)
2145{
2146 free(info);
2147}
2148
Michal Marekf64458c2013-01-16 10:18:17 +01002149static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2150{
2151 struct kmod_module_info *info;
2152 struct kmod_list *n;
2153
2154 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002155 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002156 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002157 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002158 if (n != NULL)
2159 *list = n;
2160 else
Michal Marekf64458c2013-01-16 10:18:17 +01002161 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002162 return n;
2163}
2164
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002165/**
2166 * kmod_module_get_info:
2167 * @mod: kmod module
2168 * @list: where to return list of module information. Use
2169 * kmod_module_info_get_key() and
2170 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002171 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002172 *
2173 * Get a list of entries in ELF section ".modinfo", these contain
2174 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002175 * values. If the module is signed (CONFIG_MODULE_SIG), information
2176 * about the module signature is included as well: signer,
2177 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002178 *
2179 * After use, free the @list by calling kmod_module_info_free_list().
2180 *
2181 * Returns: 0 on success or < 0 otherwise.
2182 */
2183KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2184{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002185 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002186 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002187 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002188 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002189
2190 if (mod == NULL || list == NULL)
2191 return -ENOENT;
2192
2193 assert(*list == NULL);
2194
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002195 elf = kmod_module_get_elf(mod);
2196 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002197 return -errno;
2198
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002199 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002200 if (count < 0)
2201 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002202
2203 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002204 struct kmod_list *n;
2205 const char *key, *value;
2206 size_t keylen, valuelen;
2207
2208 key = strings[i];
2209 value = strchr(key, '=');
2210 if (value == NULL) {
2211 keylen = strlen(key);
2212 valuelen = 0;
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002213 value = key;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002214 } else {
2215 keylen = value - key;
2216 value++;
2217 valuelen = strlen(value);
2218 }
2219
Michal Marekf64458c2013-01-16 10:18:17 +01002220 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2221 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002222 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002223 }
Michal Marek8fe16812013-01-16 09:52:01 +01002224
2225 if (kmod_module_signature_info(mod->file, &sig_info)) {
2226 struct kmod_list *n;
2227 char *key_hex;
2228
2229 n = kmod_module_info_append(list, "signer", strlen("signer"),
2230 sig_info.signer, sig_info.signer_len);
2231 if (n == NULL)
2232 goto list_error;
2233 count++;
2234
2235 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2236 key_hex = malloc(sig_info.key_id_len * 3);
2237 if (key_hex == NULL)
2238 goto list_error;
2239 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2240 sprintf(key_hex + i * 3, "%02X",
2241 (unsigned char)sig_info.key_id[i]);
2242 if (i < (int)sig_info.key_id_len - 1)
2243 key_hex[i * 3 + 2] = ':';
2244 }
2245 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2246 key_hex, sig_info.key_id_len * 3 - 1);
2247 free(key_hex);
2248 if (n == NULL)
2249 goto list_error;
2250 count++;
2251
2252 n = kmod_module_info_append(list,
2253 "sig_hashalgo", strlen("sig_hashalgo"),
2254 sig_info.hash_algo, strlen(sig_info.hash_algo));
2255 if (n == NULL)
2256 goto list_error;
2257 count++;
2258
2259 /*
2260 * Omit sig_info.id_type and sig_info.algo for now, as these
2261 * are currently constant.
2262 */
2263 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002264 ret = count;
2265
2266list_error:
Michal Marek63339342013-01-16 17:51:57 +01002267 if (ret < 0) {
2268 kmod_module_info_free_list(*list);
2269 *list = NULL;
2270 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002271 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002272 return ret;
2273}
2274
2275/**
2276 * kmod_module_info_get_key:
2277 * @entry: a list entry representing a kmod module info
2278 *
2279 * Get the key of a kmod module info.
2280 *
2281 * Returns: the key of this kmod module info on success or NULL on
2282 * failure. The string is owned by the info, do not free it.
2283 */
2284KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2285{
2286 struct kmod_module_info *info;
2287
2288 if (entry == NULL)
2289 return NULL;
2290
2291 info = entry->data;
2292 return info->key;
2293}
2294
2295/**
2296 * kmod_module_info_get_value:
2297 * @entry: a list entry representing a kmod module info
2298 *
2299 * Get the value of a kmod module info.
2300 *
2301 * Returns: the value of this kmod module info on success or NULL on
2302 * failure. The string is owned by the info, do not free it.
2303 */
2304KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2305{
2306 struct kmod_module_info *info;
2307
2308 if (entry == NULL)
2309 return NULL;
2310
2311 info = entry->data;
2312 return info->value;
2313}
2314
2315/**
2316 * kmod_module_info_free_list:
2317 * @list: kmod module info list
2318 *
2319 * Release the resources taken by @list
2320 */
2321KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2322{
2323 while (list) {
2324 kmod_module_info_free(list->data);
2325 list = kmod_list_remove(list);
2326 }
2327}
2328
2329struct kmod_module_version {
2330 uint64_t crc;
2331 char symbol[];
2332};
2333
2334static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2335{
2336 struct kmod_module_version *mv;
2337 size_t symbollen = strlen(symbol) + 1;
2338
2339 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2340 if (mv == NULL)
2341 return NULL;
2342
2343 mv->crc = crc;
2344 memcpy(mv->symbol, symbol, symbollen);
2345 return mv;
2346}
2347
2348static void kmod_module_version_free(struct kmod_module_version *version)
2349{
2350 free(version);
2351}
2352
2353/**
2354 * kmod_module_get_versions:
2355 * @mod: kmod module
2356 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002357 * kmod_module_version_get_symbol() and
2358 * kmod_module_version_get_crc(). Release this list with
2359 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002360 *
2361 * Get a list of entries in ELF section "__versions".
2362 *
2363 * After use, free the @list by calling kmod_module_versions_free_list().
2364 *
2365 * Returns: 0 on success or < 0 otherwise.
2366 */
2367KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2368{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002369 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002370 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002371 int i, count, ret = 0;
2372
2373 if (mod == NULL || list == NULL)
2374 return -ENOENT;
2375
2376 assert(*list == NULL);
2377
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002378 elf = kmod_module_get_elf(mod);
2379 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002380 return -errno;
2381
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002382 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002383 if (count < 0)
2384 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002385
2386 for (i = 0; i < count; i++) {
2387 struct kmod_module_version *mv;
2388 struct kmod_list *n;
2389
2390 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2391 if (mv == NULL) {
2392 ret = -errno;
2393 kmod_module_versions_free_list(*list);
2394 *list = NULL;
2395 goto list_error;
2396 }
2397
2398 n = kmod_list_append(*list, mv);
2399 if (n != NULL)
2400 *list = n;
2401 else {
2402 kmod_module_version_free(mv);
2403 kmod_module_versions_free_list(*list);
2404 *list = NULL;
2405 ret = -ENOMEM;
2406 goto list_error;
2407 }
2408 }
2409 ret = count;
2410
2411list_error:
2412 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002413 return ret;
2414}
2415
2416/**
Chengwei Yang491c4902013-05-04 17:07:02 +08002417 * kmod_module_version_get_symbol:
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002418 * @entry: a list entry representing a kmod module versions
2419 *
2420 * Get the symbol of a kmod module versions.
2421 *
2422 * Returns: the symbol of this kmod module versions on success or NULL
2423 * on failure. The string is owned by the versions, do not free it.
2424 */
2425KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2426{
2427 struct kmod_module_version *version;
2428
2429 if (entry == NULL)
2430 return NULL;
2431
2432 version = entry->data;
2433 return version->symbol;
2434}
2435
2436/**
2437 * kmod_module_version_get_crc:
2438 * @entry: a list entry representing a kmod module version
2439 *
2440 * Get the crc of a kmod module version.
2441 *
2442 * Returns: the crc of this kmod module version on success or NULL on
2443 * failure. The string is owned by the version, do not free it.
2444 */
2445KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2446{
2447 struct kmod_module_version *version;
2448
2449 if (entry == NULL)
2450 return 0;
2451
2452 version = entry->data;
2453 return version->crc;
2454}
2455
2456/**
2457 * kmod_module_versions_free_list:
2458 * @list: kmod module versions list
2459 *
2460 * Release the resources taken by @list
2461 */
2462KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2463{
2464 while (list) {
2465 kmod_module_version_free(list->data);
2466 list = kmod_list_remove(list);
2467 }
2468}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002469
2470struct kmod_module_symbol {
2471 uint64_t crc;
2472 char symbol[];
2473};
2474
2475static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2476{
2477 struct kmod_module_symbol *mv;
2478 size_t symbollen = strlen(symbol) + 1;
2479
2480 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2481 if (mv == NULL)
2482 return NULL;
2483
2484 mv->crc = crc;
2485 memcpy(mv->symbol, symbol, symbollen);
2486 return mv;
2487}
2488
2489static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2490{
2491 free(symbol);
2492}
2493
2494/**
2495 * kmod_module_get_symbols:
2496 * @mod: kmod module
2497 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002498 * kmod_module_symbol_get_symbol() and
2499 * kmod_module_symbol_get_crc(). Release this list with
2500 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002501 *
2502 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2503 *
2504 * After use, free the @list by calling kmod_module_symbols_free_list().
2505 *
2506 * Returns: 0 on success or < 0 otherwise.
2507 */
2508KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2509{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002510 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002511 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002512 int i, count, ret = 0;
2513
2514 if (mod == NULL || list == NULL)
2515 return -ENOENT;
2516
2517 assert(*list == NULL);
2518
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002519 elf = kmod_module_get_elf(mod);
2520 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002521 return -errno;
2522
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002523 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002524 if (count < 0)
2525 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002526
2527 for (i = 0; i < count; i++) {
2528 struct kmod_module_symbol *mv;
2529 struct kmod_list *n;
2530
2531 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2532 if (mv == NULL) {
2533 ret = -errno;
2534 kmod_module_symbols_free_list(*list);
2535 *list = NULL;
2536 goto list_error;
2537 }
2538
2539 n = kmod_list_append(*list, mv);
2540 if (n != NULL)
2541 *list = n;
2542 else {
2543 kmod_module_symbol_free(mv);
2544 kmod_module_symbols_free_list(*list);
2545 *list = NULL;
2546 ret = -ENOMEM;
2547 goto list_error;
2548 }
2549 }
2550 ret = count;
2551
2552list_error:
2553 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002554 return ret;
2555}
2556
2557/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002558 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002559 * @entry: a list entry representing a kmod module symbols
2560 *
2561 * Get the symbol of a kmod module symbols.
2562 *
2563 * Returns: the symbol of this kmod module symbols on success or NULL
2564 * on failure. The string is owned by the symbols, do not free it.
2565 */
2566KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2567{
2568 struct kmod_module_symbol *symbol;
2569
2570 if (entry == NULL)
2571 return NULL;
2572
2573 symbol = entry->data;
2574 return symbol->symbol;
2575}
2576
2577/**
2578 * kmod_module_symbol_get_crc:
2579 * @entry: a list entry representing a kmod module symbol
2580 *
2581 * Get the crc of a kmod module symbol.
2582 *
2583 * Returns: the crc of this kmod module symbol on success or NULL on
2584 * failure. The string is owned by the symbol, do not free it.
2585 */
2586KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2587{
2588 struct kmod_module_symbol *symbol;
2589
2590 if (entry == NULL)
2591 return 0;
2592
2593 symbol = entry->data;
2594 return symbol->crc;
2595}
2596
2597/**
2598 * kmod_module_symbols_free_list:
2599 * @list: kmod module symbols list
2600 *
2601 * Release the resources taken by @list
2602 */
2603KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2604{
2605 while (list) {
2606 kmod_module_symbol_free(list->data);
2607 list = kmod_list_remove(list);
2608 }
2609}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002610
2611struct kmod_module_dependency_symbol {
2612 uint64_t crc;
2613 uint8_t bind;
2614 char symbol[];
2615};
2616
2617static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2618{
2619 struct kmod_module_dependency_symbol *mv;
2620 size_t symbollen = strlen(symbol) + 1;
2621
2622 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2623 if (mv == NULL)
2624 return NULL;
2625
2626 mv->crc = crc;
2627 mv->bind = bind;
2628 memcpy(mv->symbol, symbol, symbollen);
2629 return mv;
2630}
2631
2632static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2633{
2634 free(dependency_symbol);
2635}
2636
2637/**
2638 * kmod_module_get_dependency_symbols:
2639 * @mod: kmod module
2640 * @list: where to return list of module dependency_symbols. Use
2641 * kmod_module_dependency_symbol_get_symbol() and
2642 * kmod_module_dependency_symbol_get_crc(). Release this list with
2643 * kmod_module_dependency_symbols_free_list()
2644 *
2645 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2646 *
2647 * After use, free the @list by calling
2648 * kmod_module_dependency_symbols_free_list().
2649 *
2650 * Returns: 0 on success or < 0 otherwise.
2651 */
2652KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2653{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002654 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002655 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002656 int i, count, ret = 0;
2657
2658 if (mod == NULL || list == NULL)
2659 return -ENOENT;
2660
2661 assert(*list == NULL);
2662
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002663 elf = kmod_module_get_elf(mod);
2664 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002665 return -errno;
2666
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002667 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002668 if (count < 0)
2669 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002670
2671 for (i = 0; i < count; i++) {
2672 struct kmod_module_dependency_symbol *mv;
2673 struct kmod_list *n;
2674
2675 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2676 symbols[i].bind,
2677 symbols[i].symbol);
2678 if (mv == NULL) {
2679 ret = -errno;
2680 kmod_module_dependency_symbols_free_list(*list);
2681 *list = NULL;
2682 goto list_error;
2683 }
2684
2685 n = kmod_list_append(*list, mv);
2686 if (n != NULL)
2687 *list = n;
2688 else {
2689 kmod_module_dependency_symbol_free(mv);
2690 kmod_module_dependency_symbols_free_list(*list);
2691 *list = NULL;
2692 ret = -ENOMEM;
2693 goto list_error;
2694 }
2695 }
2696 ret = count;
2697
2698list_error:
2699 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002700 return ret;
2701}
2702
2703/**
2704 * kmod_module_dependency_symbol_get_symbol:
2705 * @entry: a list entry representing a kmod module dependency_symbols
2706 *
2707 * Get the dependency symbol of a kmod module
2708 *
2709 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2710 * on failure. The string is owned by the dependency_symbols, do not free it.
2711 */
2712KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2713{
2714 struct kmod_module_dependency_symbol *dependency_symbol;
2715
2716 if (entry == NULL)
2717 return NULL;
2718
2719 dependency_symbol = entry->data;
2720 return dependency_symbol->symbol;
2721}
2722
2723/**
2724 * kmod_module_dependency_symbol_get_crc:
2725 * @entry: a list entry representing a kmod module dependency_symbol
2726 *
2727 * Get the crc of a kmod module dependency_symbol.
2728 *
2729 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2730 * failure. The string is owned by the dependency_symbol, do not free it.
2731 */
2732KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2733{
2734 struct kmod_module_dependency_symbol *dependency_symbol;
2735
2736 if (entry == NULL)
2737 return 0;
2738
2739 dependency_symbol = entry->data;
2740 return dependency_symbol->crc;
2741}
2742
2743/**
2744 * kmod_module_dependency_symbol_get_bind:
2745 * @entry: a list entry representing a kmod module dependency_symbol
2746 *
2747 * Get the bind type of a kmod module dependency_symbol.
2748 *
2749 * Returns: the bind of this kmod module dependency_symbol on success
2750 * or < 0 on failure.
2751 */
2752KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2753{
2754 struct kmod_module_dependency_symbol *dependency_symbol;
2755
2756 if (entry == NULL)
2757 return 0;
2758
2759 dependency_symbol = entry->data;
2760 return dependency_symbol->bind;
2761}
2762
2763/**
2764 * kmod_module_dependency_symbols_free_list:
2765 * @list: kmod module dependency_symbols list
2766 *
2767 * Release the resources taken by @list
2768 */
2769KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2770{
2771 while (list) {
2772 kmod_module_dependency_symbol_free(list->data);
2773 list = kmod_list_remove(list);
2774 }
2775}