blob: b5eb7c98deea323dc384e7b47c574897cacf9f01 [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchia66a6a92012-01-09 00:40:50 -02004 * Copyright (C) 2011-2012 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>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020036#include <sys/wait.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020037#include <string.h>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020038#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020039
40#include "libkmod.h"
41#include "libkmod-private.h"
42
43/**
Lucas De Marchi66819512012-01-09 04:47:40 -020044 * SECTION:libkmod-module
45 * @short_description: operate on kernel modules
46 */
47
48/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020049 * kmod_module:
50 *
51 * Opaque object representing a module.
52 */
53struct kmod_module {
54 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020055 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020056 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020057 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020058 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020059 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020060 const char *install_commands; /* owned by kmod_config */
61 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020062 char *alias; /* only set if this module was created from an alias */
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020063 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020064 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020065 struct {
66 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020067 bool options : 1;
68 bool install_commands : 1;
69 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020070 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020071
72 /*
73 * private field used by kmod_module_get_probe_list() to detect
74 * dependency loops
75 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020076 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020077
78 /*
79 * set by kmod_module_get_probe_list: indicates for probe_insert()
80 * whether the module's command and softdep should be ignored
81 */
82 bool ignorecmd : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020083};
84
Lucas De Marchic35347f2011-12-12 10:48:02 -020085static inline const char *path_join(const char *path, size_t prefixlen,
86 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020087{
88 size_t pathlen;
89
90 if (path[0] == '/')
91 return path;
92
93 pathlen = strlen(path);
94 if (prefixlen + pathlen + 1 >= PATH_MAX)
95 return NULL;
96
97 memcpy(buf + prefixlen, path, pathlen + 1);
98 return buf;
99}
100
Dave Reisneraf9572c2012-02-02 11:07:33 -0500101static inline bool module_is_inkernel(struct kmod_module *mod)
102{
103 int state = kmod_module_get_initstate(mod);
104 if (state == KMOD_MODULE_LIVE ||
105 state == KMOD_MODULE_COMING ||
106 state == KMOD_MODULE_BUILTIN)
107 return true;
108 else
109 return false;
110}
111
Lucas De Marchi671d4892011-12-05 20:23:05 -0200112int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200113{
114 struct kmod_ctx *ctx = mod->ctx;
115 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200116 const char *dirname;
117 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200118 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200119 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200120 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200121
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200122 if (mod->init.dep)
123 return mod->n_dep;
124 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200125 mod->init.dep = true;
126
127 p = strchr(line, ':');
128 if (p == NULL)
129 return 0;
130
Lucas De Marchi671d4892011-12-05 20:23:05 -0200131 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200132 dirname = kmod_get_dirname(mod->ctx);
133 dirnamelen = strlen(dirname);
134 if (dirnamelen + 2 >= PATH_MAX)
135 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200136
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200137 memcpy(buf, dirname, dirnamelen);
138 buf[dirnamelen] = '/';
139 dirnamelen++;
140 buf[dirnamelen] = '\0';
141
142 if (mod->path == NULL) {
143 const char *str = path_join(line, dirnamelen, buf);
144 if (str == NULL)
145 return 0;
146 mod->path = strdup(str);
147 if (mod->path == NULL)
148 return 0;
149 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200150
Lucas De Marchi7636e722011-12-01 17:56:03 -0200151 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200152 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
153 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200154 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200155 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200156
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200157 path = path_join(p, dirnamelen, buf);
158 if (path == NULL) {
159 ERR(ctx, "could not join path '%s' and '%s'.\n",
160 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200161 goto fail;
162 }
163
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200164 err = kmod_module_new_from_path(ctx, path, &depmod);
165 if (err < 0) {
166 ERR(ctx, "ctx=%p path=%s error=%s\n",
167 ctx, path, strerror(-err));
168 goto fail;
169 }
170
171 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200172
Lucas De Marchib94a7372011-12-26 20:10:49 -0200173 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200174 n++;
175 }
176
177 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
178
179 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200180 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200181 return n;
182
183fail:
184 kmod_module_unref_list(list);
185 mod->init.dep = false;
186 return err;
187}
188
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200189void kmod_module_set_visited(struct kmod_module *mod, bool visited)
190{
191 mod->visited = visited;
192}
193
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200194/*
195 * Memory layout with alias:
196 *
197 * struct kmod_module {
198 * hashkey -----.
199 * alias -----. |
200 * name ----. | |
201 * } | | |
202 * name <----------' | |
203 * alias <-----------' |
204 * name\alias <--------'
205 *
206 * Memory layout without alias:
207 *
208 * struct kmod_module {
209 * hashkey ---.
210 * alias -----|----> NULL
211 * name ----. |
212 * } | |
213 * name <----------'-'
214 *
215 * @key is "name\alias" or "name" (in which case alias == NULL)
216 */
217static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
218 const char *name, size_t namelen,
219 const char *alias, size_t aliaslen,
220 struct kmod_module **mod)
221{
222 struct kmod_module *m;
223 size_t keylen;
224
225 m = kmod_pool_get_module(ctx, key);
226 if (m != NULL) {
227 *mod = kmod_module_ref(m);
228 return 0;
229 }
230
231 if (alias == NULL)
232 keylen = namelen;
233 else
234 keylen = namelen + aliaslen + 1;
235
236 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
237 if (m == NULL) {
238 free(m);
239 return -ENOMEM;
240 }
241
242 memset(m, 0, sizeof(*m));
243
244 m->ctx = kmod_ref(ctx);
245 m->name = (char *)m + sizeof(*m);
246 memcpy(m->name, key, keylen + 1);
247 if (alias == NULL) {
248 m->hashkey = m->name;
249 m->alias = NULL;
250 } else {
251 m->name[namelen] = '\0';
252 m->alias = m->name + namelen + 1;
253 m->hashkey = m->name + keylen + 1;
254 memcpy(m->hashkey, key, keylen + 1);
255 }
256
257 m->refcount = 1;
258 kmod_pool_add_module(ctx, m, m->hashkey);
259 *mod = m;
260
261 return 0;
262}
263
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200264/**
265 * kmod_module_new_from_name:
266 * @ctx: kmod library context
267 * @name: name of the module
268 * @mod: where to save the created struct kmod_module
269 *
270 * Create a new struct kmod_module using the module name. @name can not be an
271 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600272 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200273 *
274 * This function is also used internally by many others that return a new
275 * struct kmod_module or a new list of modules.
276 *
277 * The initial refcount is 1, and needs to be decremented to release the
278 * resources of the kmod_module. Since libkmod keeps track of all
279 * kmod_modules created, they are all released upon @ctx destruction too. Do
280 * not unref @ctx before all the desired operations with the returned
281 * kmod_module are done.
282 *
283 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
284 * module name or if memory allocation failed.
285 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200286KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
287 const char *name,
288 struct kmod_module **mod)
289{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200290 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200291 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200292
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200293 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200294 return -ENOENT;
295
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200296 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200297
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200298 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200299}
300
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200301int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
302 const char *name, struct kmod_module **mod)
303{
304 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200305 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200306 size_t namelen = strlen(name);
307 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200308
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200309 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200310 return -ENAMETOOLONG;
311
312 memcpy(key, name, namelen);
313 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200314 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200315
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200316 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200317 if (err < 0)
318 return err;
319
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200320 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200321}
322
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200323/**
324 * kmod_module_new_from_path:
325 * @ctx: kmod library context
326 * @path: path where to find the given module
327 * @mod: where to save the created struct kmod_module
328 *
329 * Create a new struct kmod_module using the module path. @path must be an
330 * existent file with in the filesystem and must be accessible to libkmod.
331 *
332 * The initial refcount is 1, and needs to be decremented to release the
333 * resources of the kmod_module. Since libkmod keeps track of all
334 * kmod_modules created, they are all released upon @ctx destruction too. Do
335 * not unref @ctx before all the desired operations with the returned
336 * kmod_module are done.
337 *
338 * If @path is relative, it's treated as relative to the current working
339 * directory. Otherwise, give an absolute path.
340 *
341 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
342 * it's not a valid file for a kmod_module or if memory allocation failed.
343 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200344KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
345 const char *path,
346 struct kmod_module **mod)
347{
348 struct kmod_module *m;
349 int err;
350 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200351 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200352 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200353 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200354
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200355 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200356 return -ENOENT;
357
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200358 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200359 if (abspath == NULL) {
360 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200361 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200362 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200363
364 err = stat(abspath, &st);
365 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200366 err = -errno;
367 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200368 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200369 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200370 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200371
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200372 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200373 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200374 free(abspath);
375 return -ENOENT;
376 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200377
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200378 m = kmod_pool_get_module(ctx, name);
379 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200380 if (m->path == NULL)
381 m->path = abspath;
382 else if (streq(m->path, abspath))
383 free(abspath);
384 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200385 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 -0200386 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200387 free(abspath);
388 return -EEXIST;
389 }
390
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200391 *mod = kmod_module_ref(m);
392 return 0;
393 }
394
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200395 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
396 if (err < 0)
397 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200398
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200399 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200400 *mod = m;
401
402 return 0;
403}
404
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200405/**
406 * kmod_module_unref:
407 * @mod: kmod module
408 *
409 * Drop a reference of the kmod module. If the refcount reaches zero, its
410 * resources are released.
411 *
412 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
413 * returns the passed @mod with its refcount decremented.
414 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200415KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
416{
417 if (mod == NULL)
418 return NULL;
419
420 if (--mod->refcount > 0)
421 return mod;
422
423 DBG(mod->ctx, "kmod_module %p released\n", mod);
424
Lucas De Marchi4084c172011-12-15 13:43:22 -0200425 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200426 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200427 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200428 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200429 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200430 free(mod);
431 return NULL;
432}
433
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200434/**
435 * kmod_module_ref:
436 * @mod: kmod module
437 *
438 * Take a reference of the kmod module, incrementing its refcount.
439 *
440 * Returns: the passed @module with its refcount incremented.
441 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200442KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
443{
444 if (mod == NULL)
445 return NULL;
446
447 mod->refcount++;
448
449 return mod;
450}
451
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200452#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
453 do { \
454 if ((_err) < 0) \
455 goto _label_err; \
456 if (*(_list) != NULL) \
457 goto finish; \
458 } while (0)
459
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200460/**
461 * kmod_module_new_from_lookup:
462 * @ctx: kmod library context
463 * @given_alias: alias to look for
464 * @list: an empty list where to save the list of modules matching
465 * @given_alias
466 *
467 * Create a new list of kmod modules using an alias or module name and lookup
468 * libkmod's configuration files and indexes in order to find the module.
469 * Once it's found in one of the places, it stops searching and create the
470 * list of modules that is saved in @list.
471 *
472 * The search order is: 1. aliases in configuration file; 2. module names in
473 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
474 * in modules.alias index.
475 *
476 * The initial refcount is 1, and needs to be decremented to release the
477 * resources of the kmod_module. The returned @list must be released by
478 * calling kmod_module_unref_list(). Since libkmod keeps track of all
479 * kmod_modules created, they are all released upon @ctx destruction too. Do
480 * not unref @ctx before all the desired operations with the returned list are
481 * completed.
482 *
483 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
484 * methods failed, which is basically due to memory allocation fail. If module
485 * is not found, it still returns 0, but @list is an empty list.
486 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200487KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200488 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200489 struct kmod_list **list)
490{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200491 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200492 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200493
Lucas De Marchi4308b172011-12-13 10:26:04 -0200494 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200495 return -ENOENT;
496
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200497 if (list == NULL || *list != NULL) {
498 ERR(ctx, "An empty list is needed to create lookup\n");
499 return -ENOSYS;
500 }
501
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200502 if (alias_normalize(given_alias, alias, NULL) < 0) {
503 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200504 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200505 }
506
507 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200508
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200509 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200510 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200511 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200512
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200513 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200514 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
515 CHECK_ERR_AND_FINISH(err, fail, list, finish);
516
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200517 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200518 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
519 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200520
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200521 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200522 err = kmod_lookup_alias_from_commands(ctx, alias, list);
523 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200524
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200525 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200526 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
527 CHECK_ERR_AND_FINISH(err, fail, list, finish);
528
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200529finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200530 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200531 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200532fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200533 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200534 kmod_module_unref_list(*list);
535 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200536 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200537}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200538#undef CHECK_ERR_AND_FINISH
539
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200540/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200541 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200542 * @list: list of kmod modules
543 *
544 * Drop a reference of each kmod module in @list and releases the resources
545 * taken by the list itself.
546 *
547 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
548 * returns the passed @mod with its refcount decremented.
549 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200550KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
551{
552 for (; list != NULL; list = kmod_list_remove(list))
553 kmod_module_unref(list->data);
554
555 return 0;
556}
557
Lucas De Marchi0d467432011-12-31 11:15:52 -0200558/**
559 * kmod_module_get_filtered_blacklist:
560 * @ctx: kmod library context
561 * @input: list of kmod_module to be filtered with blacklist
562 * @output: where to save the new list
563 *
564 * Given a list @input, this function filter it out with config's blacklist
565 * ans save it in @output.
566 *
567 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
568 * list.
569 */
570KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
571 const struct kmod_list *input,
572 struct kmod_list **output)
573{
574 const struct kmod_list *li;
575 const struct kmod_list *blacklist;
576
577 if (ctx == NULL || output == NULL)
578 return -ENOENT;
579
580 *output = NULL;
581 if (input == NULL)
582 return 0;
583
584 blacklist = kmod_get_blacklists(ctx);
585 kmod_list_foreach(li, input) {
586 struct kmod_module *mod = li->data;
587 const struct kmod_list *lb;
588 struct kmod_list *node;
589 bool filtered = false;
590
591 kmod_list_foreach(lb, blacklist) {
592 const char *name = lb->data;
593
Lucas De Marchi4926cb52011-12-31 11:21:52 -0200594 if (streq(name, mod->name)) {
Lucas De Marchi0d467432011-12-31 11:15:52 -0200595 filtered = true;
596 break;
597 }
598 }
599
600 if (filtered)
601 continue;
602
603 node = kmod_list_append(*output, mod);
604 if (node == NULL)
605 goto fail;
606
607 *output = node;
608 kmod_module_ref(mod);
609 }
610
611 return 0;
612
613fail:
614 kmod_module_unref_list(*output);
615 *output = NULL;
616 return -ENOMEM;
617}
618
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200619static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
620{
621 if (!mod->init.dep) {
622 /* lazy init */
623 char *line = kmod_search_moddep(mod->ctx, mod->name);
624
625 if (line == NULL)
626 return NULL;
627
628 kmod_module_parse_depline((struct kmod_module *)mod, line);
629 free(line);
630
631 if (!mod->init.dep)
632 return NULL;
633 }
634
635 return mod->dep;
636}
637
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200638/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200639 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200640 * @mod: kmod module
641 *
642 * Search the modules.dep index to find the dependencies of the given @mod.
643 * The result is cached in @mod, so subsequent calls to this function will
644 * return the already searched list of modules.
645 *
646 * Returns: NULL on failure or if there are any dependencies. Otherwise it
647 * returns a list of kmod modules that can be released by calling
648 * kmod_module_unref_list().
649 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200650KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200651{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200652 struct kmod_list *l, *l_new, *list_new = NULL;
653
654 if (mod == NULL)
655 return NULL;
656
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200657 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200658
659 kmod_list_foreach(l, mod->dep) {
660 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
661 if (l_new == NULL) {
662 kmod_module_unref(l->data);
663 goto fail;
664 }
665
666 list_new = l_new;
667 }
668
669 return list_new;
670
671fail:
672 ERR(mod->ctx, "out of memory\n");
673 kmod_module_unref_list(list_new);
674 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200675}
676
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200677/**
678 * kmod_module_get_module:
679 * @entry: an entry in a list of kmod modules.
680 *
681 * Get the kmod module of this @entry in the list, increasing its refcount.
682 * After it's used, unref it. Since the refcount is incremented upon return,
683 * you still have to call kmod_module_unref_list() to release the list of kmod
684 * modules.
685 *
686 * Returns: NULL on failure or the kmod_module contained in this list entry
687 * with its refcount incremented.
688 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200689KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200690{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200691 if (entry == NULL)
692 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200693
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200694 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200695}
696
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200697/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200698 * kmod_module_get_name:
699 * @mod: kmod module
700 *
701 * Get the name of this kmod module. Name is always available, independently
702 * if it was created by kmod_module_new_from_name() or another function and
703 * it's always normalized (dashes are replaced with underscores).
704 *
705 * Returns: the name of this kmod module.
706 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200707KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200708{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200709 if (mod == NULL)
710 return NULL;
711
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200712 return mod->name;
713}
714
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200715/**
716 * kmod_module_get_path:
717 * @mod: kmod module
718 *
719 * Get the path of this kmod module. If this kmod module was not created by
720 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200721 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200722 *
723 * Returns: the path of this kmod module or NULL if such information is not
724 * available.
725 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200726KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200727{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200728 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200729
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200730 if (mod == NULL)
731 return NULL;
732
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200733 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200734
Lucas De Marchie005fac2011-12-08 10:42:34 -0200735 if (mod->path != NULL)
736 return mod->path;
737 if (mod->init.dep)
738 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200739
Lucas De Marchie005fac2011-12-08 10:42:34 -0200740 /* lazy init */
741 line = kmod_search_moddep(mod->ctx, mod->name);
742 if (line == NULL)
743 return NULL;
744
745 kmod_module_parse_depline((struct kmod_module *) mod, line);
746 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200747
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200748 return mod->path;
749}
750
751
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200752extern long delete_module(const char *name, unsigned int flags);
753
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200754/**
755 * kmod_module_remove_module:
756 * @mod: kmod module
757 * @flags: flags to pass to Linux kernel when removing the module
758 *
759 * Remove a module from Linux kernel.
760 *
761 * Returns: 0 on success or < 0 on failure.
762 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200763KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
764 unsigned int flags)
765{
766 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200767
768 if (mod == NULL)
769 return -ENOENT;
770
771 /* Filter out other flags */
772 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
773
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200774 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200775 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200776 err = -errno;
777 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200778 }
779
Lucas De Marchiba998b92012-01-11 00:08:14 -0200780 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200781}
782
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200783extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200784
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200785/**
786 * kmod_module_insert_module:
787 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200788 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
789 * behavior of this function.
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;
805 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200806 struct kmod_elf *elf = NULL;
807 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200808 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200809
810 if (mod == NULL)
811 return -ENOENT;
812
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200813 path = kmod_module_get_path(mod);
814 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500815 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200816 return -ENOSYS;
817 }
818
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200819 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200820 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200821 err = -errno;
822 return err;
823 }
824
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200825 size = kmod_file_get_size(file);
826 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200827
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200828 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
829 elf = kmod_elf_new(mem, size);
830 if (elf == NULL) {
831 err = -errno;
832 goto elf_failed;
833 }
834
835 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
836 err = kmod_elf_strip_section(elf, "__versions");
837 if (err < 0)
838 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
839 }
840
841 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
842 err = kmod_elf_strip_vermagic(elf);
843 if (err < 0)
844 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
845 }
846
847 mem = kmod_elf_get_memory(elf);
848 }
849
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200850 err = init_module(mem, size, args);
Lucas De Marchibbf59322011-12-30 14:13:33 -0200851 if (err < 0) {
852 err = -errno;
853 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
854 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200855
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200856 if (elf != NULL)
857 kmod_elf_unref(elf);
858elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200859 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200860
861 return err;
862}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200863
Lucas De Marchiddbda022011-12-27 11:40:10 -0200864static bool module_is_blacklisted(struct kmod_module *mod)
865{
866 struct kmod_ctx *ctx = mod->ctx;
867 const struct kmod_list *bl = kmod_get_blacklists(ctx);
868 const struct kmod_list *l;
869
870 kmod_list_foreach(l, bl) {
871 const char *modname = kmod_blacklist_get_modname(l);
872
873 if (streq(modname, mod->name))
874 return true;
875 }
876
877 return false;
878}
879
Lucas De Marchiddbda022011-12-27 11:40:10 -0200880static int command_do(struct kmod_module *mod, const char *type,
881 const char *cmd)
882{
883 const char *modname = kmod_module_get_name(mod);
884 int err;
885
886 DBG(mod->ctx, "%s %s\n", type, cmd);
887
888 setenv("MODPROBE_MODULE", modname, 1);
889 err = system(cmd);
890 unsetenv("MODPROBE_MODULE");
891
892 if (err == -1 || WEXITSTATUS(err)) {
893 ERR(mod->ctx, "Error running %s command for %s\n",
894 type, modname);
895 if (err != -1)
896 err = -WEXITSTATUS(err);
897 }
898
899 return err;
900}
901
Lucas De Marchib1a51252012-01-29 01:49:09 -0200902struct probe_insert_cb {
903 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
904 void *data;
905};
906
Lucas De Marchiddbda022011-12-27 11:40:10 -0200907static int module_do_install_commands(struct kmod_module *mod,
908 const char *options,
909 struct probe_insert_cb *cb)
910{
911 const char *command = kmod_module_get_install_commands(mod);
912 char *p, *cmd;
913 int err;
914 size_t cmdlen, options_len, varlen;
915
916 assert(command);
917
918 if (options == NULL)
919 options = "";
920
921 options_len = strlen(options);
922 cmdlen = strlen(command);
923 varlen = sizeof("$CMDLINE_OPTS") - 1;
924
925 cmd = memdup(command, cmdlen + 1);
926 if (cmd == NULL)
927 return -ENOMEM;
928
929 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
930 size_t prefixlen = p - cmd;
931 size_t suffixlen = cmdlen - prefixlen - varlen;
932 size_t slen = cmdlen - varlen + options_len;
933 char *suffix = p + varlen;
934 char *s = malloc(slen + 1);
935 if (s == NULL) {
936 free(cmd);
937 return -ENOMEM;
938 }
939 memcpy(s, cmd, p - cmd);
940 memcpy(s + prefixlen, options, options_len);
941 memcpy(s + prefixlen + options_len, suffix, suffixlen);
942 s[slen] = '\0';
943
944 free(cmd);
945 cmd = s;
946 cmdlen = slen;
947 }
948
949 if (cb->run_install != NULL)
950 err = cb->run_install(mod, cmd, cb->data);
951 else
952 err = command_do(mod, "install", cmd);
953
954 free(cmd);
955
956 return err;
957}
958
Lucas De Marchiddbda022011-12-27 11:40:10 -0200959static char *module_options_concat(const char *opt, const char *xopt)
960{
961 // TODO: we might need to check if xopt overrides options on opt
962 size_t optlen = opt == NULL ? 0 : strlen(opt);
963 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
964 char *r;
965
966 if (optlen == 0 && xoptlen == 0)
967 return NULL;
968
969 r = malloc(optlen + xoptlen + 2);
970
971 if (opt != NULL) {
972 memcpy(r, opt, optlen);
973 r[optlen] = ' ';
974 optlen++;
975 }
976
977 if (xopt != NULL)
978 memcpy(r + optlen, xopt, xoptlen);
979
980 r[optlen + xoptlen] = '\0';
981
982 return r;
983}
984
Lucas De Marchib1a51252012-01-29 01:49:09 -0200985static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -0200986 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -0200987 struct kmod_list **list);
988
989/* re-entrant */
990static int __kmod_module_fill_softdep(struct kmod_module *mod,
991 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -0200992{
Lucas De Marchib1a51252012-01-29 01:49:09 -0200993 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200994 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200995
996 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -0200997 if (err < 0) {
998 ERR(mod->ctx, "could not get softdep: %s", strerror(-err));
999 goto fail;
1000 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001001
Lucas De Marchib1a51252012-01-29 01:49:09 -02001002 kmod_list_foreach(l, pre) {
1003 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001004 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001005 if (err < 0)
1006 goto fail;
1007 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001008
Lucas De Marchib1a51252012-01-29 01:49:09 -02001009 l = kmod_list_append(*list, kmod_module_ref(mod));
1010 if (l == NULL) {
1011 kmod_module_unref(mod);
1012 err = -ENOMEM;
1013 goto fail;
1014 }
1015 *list = l;
1016 mod->visited = true;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001017 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001018
Lucas De Marchib1a51252012-01-29 01:49:09 -02001019 kmod_list_foreach(l, post) {
1020 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001021 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001022 if (err < 0)
1023 goto fail;
1024 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001025
Lucas De Marchib1a51252012-01-29 01:49:09 -02001026fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001027 kmod_module_unref_list(pre);
1028 kmod_module_unref_list(post);
1029
1030 return err;
1031}
1032
Lucas De Marchib1a51252012-01-29 01:49:09 -02001033/* re-entrant */
1034static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001035 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001036 struct kmod_list **list)
1037{
1038 struct kmod_list *dep, *l;
1039 int err = 0;
1040
1041 if (mod->visited) {
1042 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1043 mod->name);
1044 return 0;
1045 }
1046
1047 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001048 kmod_list_foreach(l, dep) {
1049 struct kmod_module *m = l->data;
1050 err = __kmod_module_fill_softdep(m, list);
1051 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001052 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001053 }
1054
Lucas De Marchi89e92482012-01-29 02:35:46 -02001055 if (ignorecmd) {
1056 l = kmod_list_append(*list, kmod_module_ref(mod));
1057 if (l == NULL) {
1058 kmod_module_unref(mod);
1059 err = -ENOMEM;
1060 goto finish;
1061 }
1062 *list = l;
1063 mod->ignorecmd = true;
1064 } else
1065 err = __kmod_module_fill_softdep(mod, list);
1066
Lucas De Marchib1a51252012-01-29 01:49:09 -02001067finish:
1068 kmod_module_unref_list(dep);
1069 return err;
1070}
1071
1072static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001073 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001074 struct kmod_list **list)
1075{
1076 int err;
1077
1078 assert(mod != NULL);
1079 assert(list != NULL && *list == NULL);
1080
1081 /*
1082 * Make sure we don't get screwed by previous calls to this function
1083 */
1084 kmod_set_modules_visited(mod->ctx, false);
1085
Lucas De Marchi89e92482012-01-29 02:35:46 -02001086 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001087 if (err < 0) {
1088 kmod_module_unref_list(*list);
1089 *list = NULL;
1090 }
1091
1092 return err;
1093}
1094
Lucas De Marchiddbda022011-12-27 11:40:10 -02001095/**
1096 * kmod_module_probe_insert_module:
1097 * @mod: kmod module
1098 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1099 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001100 * @extra_options: module's options to pass to Linux Kernel. It applies only
1101 * to @mod, not to its dependencies.
1102 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001103 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001104 * @print_action: function to call with the action being taken (install or
1105 * insmod). It's useful for tools like modprobe when running with verbose
1106 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001107 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001108 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001109 * install commands and applying blacklist.
1110 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001111 * If @run_install is NULL, this function will fork and exec by calling
1112 * system(3). Don't pass a NULL argument in @run_install if your binary is
1113 * setuid/setgid (see warning in system(3)). If you need control over the
1114 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001115 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001116 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1117 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001118 */
1119KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1120 unsigned int flags, const char *extra_options,
1121 int (*run_install)(struct kmod_module *m,
1122 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001123 const void *data,
1124 void (*print_action)(struct kmod_module *m,
1125 bool install,
1126 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001127{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001128 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001129 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001130 int err;
1131
1132 if (mod == NULL)
1133 return -ENOENT;
1134
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001135 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1136 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001137 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001138 return -EEXIST;
1139 else
1140 return 0;
1141 }
1142
Lucas De Marchib1a51252012-01-29 01:49:09 -02001143 err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
1144 KMOD_PROBE_APPLY_BLACKLIST_ALL);
1145 if (err != 0) {
1146 if (module_is_blacklisted(mod))
1147 return err;
1148 }
1149
Lucas De Marchi89e92482012-01-29 02:35:46 -02001150 err = kmod_module_get_probe_list(mod,
1151 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001152 if (err < 0)
1153 return err;
1154
1155 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1156 struct kmod_list *filtered = NULL;
1157
1158 err = kmod_module_get_filtered_blacklist(mod->ctx,
1159 list, &filtered);
1160 if (err < 0)
1161 return err;
1162
1163 kmod_module_unref_list(list);
1164 if (filtered == NULL)
1165 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1166
1167 list = filtered;
1168 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001169
1170 cb.run_install = run_install;
1171 cb.data = (void *) data;
1172
Lucas De Marchib1a51252012-01-29 01:49:09 -02001173 kmod_list_foreach(l, list) {
1174 struct kmod_module *m = l->data;
1175 const char *moptions = kmod_module_get_options(m);
1176 const char *cmd = kmod_module_get_install_commands(m);
1177 char *options = module_options_concat(moptions,
1178 m == mod ? extra_options : NULL);
1179
Lucas De Marchi89e92482012-01-29 02:35:46 -02001180 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001181 if (print_action != NULL)
1182 print_action(m, true, options ?: "");
1183
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001184 if (!(flags & KMOD_PROBE_DRY_RUN))
1185 err = module_do_install_commands(m, options,
1186 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001187 } else {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001188 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1189 && module_is_inkernel(m)) {
Lucas De Marchi7c10c692012-01-30 18:54:45 -02001190 DBG(mod->ctx, "Ignoring module '%s': "
1191 "already loaded\n", m->name);
Lucas De Marchi08600ee2012-02-06 12:52:27 -02001192 err = -EEXIST;
1193 goto finish_module;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001194 }
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001195 if (print_action != NULL)
1196 print_action(m, false, options ?: "");
1197
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001198 if (!(flags & KMOD_PROBE_DRY_RUN))
1199 err = kmod_module_insert_module(m, flags,
1200 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001201 }
1202
Lucas De Marchi08600ee2012-02-06 12:52:27 -02001203finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001204 free(options);
1205
1206 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001207 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001208 * already loaded and the module being loaded is not a softdep
1209 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001210 *
1211 * We need to check here because of race conditions. We
1212 * checked first if module was already loaded but it may have
1213 * been loaded between the check and the moment we try to
1214 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001215 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001216 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001217 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001218 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001219
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001220 if (err == -EEXIST)
1221 err = 0;
1222 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001223 break;
1224 }
1225
1226 kmod_module_unref_list(list);
1227 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001228}
1229
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001230/**
1231 * kmod_module_get_options:
1232 * @mod: kmod module
1233 *
1234 * Get options of this kmod module. Options come from the configuration file
1235 * and are cached in @mod. The first call to this function will search for
1236 * this module in configuration and subsequent calls return the cached string.
1237 *
1238 * Returns: a string with all the options separated by spaces. This string is
1239 * owned by @mod, do not free it.
1240 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001241KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1242{
1243 if (mod == NULL)
1244 return NULL;
1245
1246 if (!mod->init.options) {
1247 /* lazy init */
1248 struct kmod_module *m = (struct kmod_module *)mod;
1249 const struct kmod_list *l, *ctx_options;
1250 char *opts = NULL;
1251 size_t optslen = 0;
1252
1253 ctx_options = kmod_get_options(mod->ctx);
1254
1255 kmod_list_foreach(l, ctx_options) {
1256 const char *modname = kmod_option_get_modname(l);
1257 const char *str;
1258 size_t len;
1259 void *tmp;
1260
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001261 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1262 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1263 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001264 continue;
1265
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001266 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 -02001267 str = kmod_option_get_options(l);
1268 len = strlen(str);
1269 if (len < 1)
1270 continue;
1271
1272 tmp = realloc(opts, optslen + len + 2);
1273 if (tmp == NULL) {
1274 free(opts);
1275 goto failed;
1276 }
1277
1278 opts = tmp;
1279
1280 if (optslen > 0) {
1281 opts[optslen] = ' ';
1282 optslen++;
1283 }
1284
1285 memcpy(opts + optslen, str, len);
1286 optslen += len;
1287 opts[optslen] = '\0';
1288 }
1289
1290 m->init.options = true;
1291 m->options = opts;
1292 }
1293
1294 return mod->options;
1295
1296failed:
1297 ERR(mod->ctx, "out of memory\n");
1298 return NULL;
1299}
1300
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001301/**
1302 * kmod_module_get_install_commands:
1303 * @mod: kmod module
1304 *
1305 * Get install commands for this kmod module. Install commands come from the
1306 * configuration file and are cached in @mod. The first call to this function
1307 * will search for this module in configuration and subsequent calls return
1308 * the cached string. The install commands are returned as they were in the
1309 * configuration, concatenated by ';'. No other processing is made in this
1310 * string.
1311 *
1312 * Returns: a string with all install commands separated by semicolons. This
1313 * string is owned by @mod, do not free it.
1314 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001315KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1316{
1317 if (mod == NULL)
1318 return NULL;
1319
1320 if (!mod->init.install_commands) {
1321 /* lazy init */
1322 struct kmod_module *m = (struct kmod_module *)mod;
1323 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001324
1325 ctx_install_commands = kmod_get_install_commands(mod->ctx);
1326
1327 kmod_list_foreach(l, ctx_install_commands) {
1328 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001329
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001330 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001331 continue;
1332
Lucas De Marchi60f67602011-12-16 03:33:26 -02001333 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001334
Lucas De Marchi60f67602011-12-16 03:33:26 -02001335 /*
1336 * find only the first command, as modprobe from
1337 * module-init-tools does
1338 */
1339 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001340 }
1341
1342 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001343 }
1344
1345 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001346}
1347
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001348void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1349{
1350 mod->init.install_commands = true;
1351 mod->install_commands = cmd;
1352}
1353
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001354static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1355{
1356 struct kmod_list *ret = NULL;
1357 unsigned i;
1358
1359 for (i = 0; i < count; i++) {
1360 const char *depname = array[i];
1361 struct kmod_list *lst = NULL;
1362 int err;
1363
1364 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1365 if (err < 0) {
1366 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1367 continue;
1368 } else if (lst != NULL)
1369 ret = kmod_list_append_list(ret, lst);
1370 }
1371 return ret;
1372}
1373
1374/**
1375 * kmod_module_get_softdeps:
1376 * @mod: kmod module
1377 * @pre: where to save the list of preceding soft dependencies.
1378 * @post: where to save the list of post soft dependencies.
1379 *
1380 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001381 * from configuration file and are not cached in @mod because it may include
1382 * dependency cycles that would make we leak kmod_module. Any call
1383 * to this function will search for this module in configuration, allocate a
1384 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001385 *
1386 * Both @pre and @post are newly created list of kmod_module and
1387 * should be unreferenced with kmod_module_unref_list().
1388 *
1389 * Returns: 0 on success or < 0 otherwise.
1390 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001391KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1392 struct kmod_list **pre,
1393 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001394{
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001395 const struct kmod_list *l, *ctx_softdeps;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001396
1397 if (mod == NULL || pre == NULL || post == NULL)
1398 return -ENOENT;
1399
1400 assert(*pre == NULL);
1401 assert(*post == NULL);
1402
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001403 ctx_softdeps = kmod_get_softdeps(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001404
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001405 kmod_list_foreach(l, ctx_softdeps) {
1406 const char *modname = kmod_softdep_get_name(l);
1407 const char * const *array;
1408 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001409
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001410 if (fnmatch(modname, mod->name, 0) != 0)
1411 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001412
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001413 array = kmod_softdep_get_pre(l, &count);
1414 *pre = lookup_softdep(mod->ctx, array, count);
1415 array = kmod_softdep_get_post(l, &count);
1416 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001417
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001418 /*
1419 * find only the first command, as modprobe from
1420 * module-init-tools does
1421 */
1422 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001423 }
1424
1425 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001426}
1427
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001428/**
1429 * kmod_module_get_remove_commands:
1430 * @mod: kmod module
1431 *
1432 * Get remove commands for this kmod module. Remove commands come from the
1433 * configuration file and are cached in @mod. The first call to this function
1434 * will search for this module in configuration and subsequent calls return
1435 * the cached string. The remove commands are returned as they were in the
1436 * configuration, concatenated by ';'. No other processing is made in this
1437 * string.
1438 *
1439 * Returns: a string with all remove commands separated by semicolons. This
1440 * string is owned by @mod, do not free it.
1441 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001442KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1443{
1444 if (mod == NULL)
1445 return NULL;
1446
1447 if (!mod->init.remove_commands) {
1448 /* lazy init */
1449 struct kmod_module *m = (struct kmod_module *)mod;
1450 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001451
1452 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1453
1454 kmod_list_foreach(l, ctx_remove_commands) {
1455 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001456
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001457 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001458 continue;
1459
Lucas De Marchi60f67602011-12-16 03:33:26 -02001460 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001461
Lucas De Marchi60f67602011-12-16 03:33:26 -02001462 /*
1463 * find only the first command, as modprobe from
1464 * module-init-tools does
1465 */
1466 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001467 }
1468
1469 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001470 }
1471
1472 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001473}
1474
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001475void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1476{
1477 mod->init.remove_commands = true;
1478 mod->remove_commands = cmd;
1479}
1480
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001481/**
1482 * SECTION:libkmod-loaded
1483 * @short_description: currently loaded modules
1484 *
1485 * Information about currently loaded modules, as reported by Linux kernel.
1486 * These information are not cached by libkmod and are always read from /sys
1487 * and /proc/modules.
1488 */
1489
1490/**
1491 * kmod_module_new_from_loaded:
1492 * @ctx: kmod library context
1493 * @list: where to save the list of loaded modules
1494 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001495 * Create a new list of kmod modules with all modules currently loaded in
1496 * kernel. It uses /proc/modules to get the names of loaded modules and to
1497 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1498 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001499 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001500 * The initial refcount is 1, and needs to be decremented to release the
1501 * resources of the kmod_module. The returned @list must be released by
1502 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1503 * kmod_modules created, they are all released upon @ctx destruction too. Do
1504 * not unref @ctx before all the desired operations with the returned list are
1505 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001506 *
1507 * Returns: 0 on success or < 0 on error.
1508 */
1509KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1510 struct kmod_list **list)
1511{
1512 struct kmod_list *l = NULL;
1513 FILE *fp;
1514 char line[4096];
1515
1516 if (ctx == NULL || list == NULL)
1517 return -ENOENT;
1518
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001519 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001520 if (fp == NULL) {
1521 int err = -errno;
1522 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1523 return err;
1524 }
1525
1526 while (fgets(line, sizeof(line), fp)) {
1527 struct kmod_module *m;
1528 struct kmod_list *node;
1529 int err;
1530 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1531
1532 err = kmod_module_new_from_name(ctx, name, &m);
1533 if (err < 0) {
1534 ERR(ctx, "could not get module from name '%s': %s\n",
1535 name, strerror(-err));
1536 continue;
1537 }
1538
1539 node = kmod_list_append(l, m);
1540 if (node)
1541 l = node;
1542 else {
1543 ERR(ctx, "out of memory\n");
1544 kmod_module_unref(m);
1545 }
1546 }
1547
1548 fclose(fp);
1549 *list = l;
1550
1551 return 0;
1552}
1553
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001554/**
1555 * kmod_module_initstate_str:
1556 * @state: the state as returned by kmod_module_get_initstate()
1557 *
1558 * Translate a initstate to a string.
1559 *
1560 * Returns: the string associated to the @state. This string is statically
1561 * allocated, do not free it.
1562 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001563KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1564{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001565 switch (state) {
1566 case KMOD_MODULE_BUILTIN:
1567 return "builtin";
1568 case KMOD_MODULE_LIVE:
1569 return "live";
1570 case KMOD_MODULE_COMING:
1571 return "coming";
1572 case KMOD_MODULE_GOING:
1573 return "going";
1574 default:
1575 return NULL;
1576 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001577}
1578
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001579/**
1580 * kmod_module_get_initstate:
1581 * @mod: kmod module
1582 *
1583 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1584 * /sys filesystem.
1585 *
1586 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1587 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001588KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1589{
1590 char path[PATH_MAX], buf[32];
1591 int fd, err, pathlen;
1592
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001593 if (mod == NULL)
1594 return -ENOENT;
1595
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001596 pathlen = snprintf(path, sizeof(path),
1597 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001598 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001599 if (fd < 0) {
1600 err = -errno;
1601
Lucas De Marchiddbda022011-12-27 11:40:10 -02001602 DBG(mod->ctx, "could not open '%s': %s\n",
1603 path, strerror(-err));
1604
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001605 if (pathlen > (int)sizeof("/initstate") - 1) {
1606 struct stat st;
1607 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1608 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1609 return KMOD_MODULE_BUILTIN;
1610 }
1611
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001612 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001613 path, strerror(-err));
1614 return err;
1615 }
1616
1617 err = read_str_safe(fd, buf, sizeof(buf));
1618 close(fd);
1619 if (err < 0) {
1620 ERR(mod->ctx, "could not read from '%s': %s\n",
1621 path, strerror(-err));
1622 return err;
1623 }
1624
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001625 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001626 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001627 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001628 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001629 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001630 return KMOD_MODULE_GOING;
1631
1632 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1633 return -EINVAL;
1634}
1635
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001636/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001637 * kmod_module_get_size:
1638 * @mod: kmod module
1639 *
1640 * Get the size of this kmod module as returned by Linux kernel. It reads the
1641 * file /proc/modules to search for this module and get its size.
1642 *
1643 * Returns: the size of this kmod module.
1644 */
1645KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1646{
1647 // FIXME TODO: this should be available from /sys/module/foo
1648 FILE *fp;
1649 char line[4096];
1650 int lineno = 0;
1651 long size = -ENOENT;
1652
1653 if (mod == NULL)
1654 return -ENOENT;
1655
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001656 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001657 if (fp == NULL) {
1658 int err = -errno;
1659 ERR(mod->ctx,
1660 "could not open /proc/modules: %s\n", strerror(errno));
1661 return err;
1662 }
1663
1664 while (fgets(line, sizeof(line), fp)) {
1665 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1666 long value;
1667
1668 lineno++;
1669 if (tok == NULL || !streq(tok, mod->name))
1670 continue;
1671
1672 tok = strtok_r(NULL, " \t", &saveptr);
1673 if (tok == NULL) {
1674 ERR(mod->ctx,
1675 "invalid line format at /proc/modules:%d\n", lineno);
1676 break;
1677 }
1678
1679 value = strtol(tok, &endptr, 10);
1680 if (endptr == tok || *endptr != '\0') {
1681 ERR(mod->ctx,
1682 "invalid line format at /proc/modules:%d\n", lineno);
1683 break;
1684 }
1685
1686 size = value;
1687 break;
1688 }
1689 fclose(fp);
1690 return size;
1691}
1692
1693/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001694 * kmod_module_get_refcnt:
1695 * @mod: kmod module
1696 *
1697 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1698 * /sys filesystem.
1699 *
1700 * Returns: 0 on success or < 0 on failure.
1701 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001702KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1703{
1704 char path[PATH_MAX];
1705 long refcnt;
1706 int fd, err;
1707
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001708 if (mod == NULL)
1709 return -ENOENT;
1710
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001711 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001712 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001713 if (fd < 0) {
1714 err = -errno;
1715 ERR(mod->ctx, "could not open '%s': %s\n",
1716 path, strerror(errno));
1717 return err;
1718 }
1719
1720 err = read_str_long(fd, &refcnt, 10);
1721 close(fd);
1722 if (err < 0) {
1723 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1724 path, strerror(-err));
1725 return err;
1726 }
1727
1728 return (int)refcnt;
1729}
1730
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001731/**
1732 * kmod_module_get_holders:
1733 * @mod: kmod module
1734 *
1735 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1736 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1737 *
1738 * Returns: a new list of kmod modules on success or NULL on failure.
1739 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001740KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1741{
1742 char dname[PATH_MAX];
1743 struct kmod_list *list = NULL;
1744 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001745
1746 if (mod == NULL)
1747 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001748
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001749 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1750
1751 d = opendir(dname);
1752 if (d == NULL) {
1753 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001754 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001755 return NULL;
1756 }
1757
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001758 for (;;) {
1759 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001760 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001761 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001762 int err;
1763
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001764 err = readdir_r(d, &de, &entp);
1765 if (err != 0) {
1766 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1767 mod->name, strerror(-err));
1768 goto fail;
1769 }
1770
1771 if (entp == NULL)
1772 break;
1773
1774 if (de.d_name[0] == '.') {
1775 if (de.d_name[1] == '\0' ||
1776 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001777 continue;
1778 }
1779
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001780 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001781 if (err < 0) {
1782 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001783 de.d_name, strerror(-err));
1784 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001785 }
1786
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001787 l = kmod_list_append(list, holder);
1788 if (l != NULL) {
1789 list = l;
1790 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001791 ERR(mod->ctx, "out of memory\n");
1792 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001793 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001794 }
1795 }
1796
1797 closedir(d);
1798 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001799
1800fail:
1801 closedir(d);
1802 kmod_module_unref_list(list);
1803 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001804}
1805
1806struct kmod_module_section {
1807 unsigned long address;
1808 char name[];
1809};
1810
1811static void kmod_module_section_free(struct kmod_module_section *section)
1812{
1813 free(section);
1814}
1815
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001816/**
1817 * kmod_module_get_sections:
1818 * @mod: kmod module
1819 *
1820 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1821 * structure contained in this list is internal to libkmod and their fields
1822 * can be obtained by calling kmod_module_section_get_name() and
1823 * kmod_module_section_get_address().
1824 *
1825 * After use, free the @list by calling kmod_module_section_free_list().
1826 *
1827 * Returns: a new list of kmod module sections on success or NULL on failure.
1828 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001829KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1830{
1831 char dname[PATH_MAX];
1832 struct kmod_list *list = NULL;
1833 DIR *d;
1834 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001835
1836 if (mod == NULL)
1837 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001838
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001839 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1840
1841 d = opendir(dname);
1842 if (d == NULL) {
1843 ERR(mod->ctx, "could not open '%s': %s\n",
1844 dname, strerror(errno));
1845 return NULL;
1846 }
1847
1848 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001849
1850 for (;;) {
1851 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001852 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001853 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001854 unsigned long address;
1855 size_t namesz;
1856 int fd, err;
1857
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001858 err = readdir_r(d, &de, &entp);
1859 if (err != 0) {
1860 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1861 mod->name, strerror(-err));
1862 goto fail;
1863 }
1864
1865 if (de.d_name[0] == '.') {
1866 if (de.d_name[1] == '\0' ||
1867 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001868 continue;
1869 }
1870
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001871 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001872 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001873 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1874 dname, de.d_name);
1875 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001876 }
1877
1878 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001879 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001880 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001881 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1882 dname, de.d_name);
1883 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001884 }
1885
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001886 namesz = strlen(de.d_name) + 1;
1887 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001888
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001889 if (section == NULL) {
1890 ERR(mod->ctx, "out of memory\n");
1891 goto fail;
1892 }
1893
1894 section->address = address;
1895 memcpy(section->name, de.d_name, namesz);
1896
1897 l = kmod_list_append(list, section);
1898 if (l != NULL) {
1899 list = l;
1900 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001901 ERR(mod->ctx, "out of memory\n");
1902 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001903 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001904 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001905 }
1906
1907 closedir(d);
1908 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001909
1910fail:
1911 closedir(d);
1912 kmod_module_unref_list(list);
1913 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001914}
1915
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001916/**
1917 * kmod_module_section_get_module_name:
1918 * @entry: a list entry representing a kmod module section
1919 *
1920 * Get the name of a kmod module section.
1921 *
1922 * After use, free the @list by calling kmod_module_section_free_list().
1923 *
1924 * Returns: the name of this kmod module section on success or NULL on
1925 * failure. The string is owned by the section, do not free it.
1926 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001927KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1928{
1929 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001930
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001931 if (entry == NULL)
1932 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001933
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001934 section = entry->data;
1935 return section->name;
1936}
1937
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001938/**
1939 * kmod_module_section_get_address:
1940 * @entry: a list entry representing a kmod module section
1941 *
1942 * Get the address of a kmod module section.
1943 *
1944 * After use, free the @list by calling kmod_module_section_free_list().
1945 *
1946 * Returns: the address of this kmod module section on success or ULONG_MAX
1947 * on failure.
1948 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001949KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1950{
1951 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001952
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001953 if (entry == NULL)
1954 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001955
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001956 section = entry->data;
1957 return section->address;
1958}
1959
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001960/**
1961 * kmod_module_section_free_list:
1962 * @list: kmod module section list
1963 *
1964 * Release the resources taken by @list
1965 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001966KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1967{
1968 while (list) {
1969 kmod_module_section_free(list->data);
1970 list = kmod_list_remove(list);
1971 }
1972}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001973
1974struct kmod_module_info {
1975 char *key;
1976 char value[];
1977};
1978
1979static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
1980{
1981 struct kmod_module_info *info;
1982
1983 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
1984 if (info == NULL)
1985 return NULL;
1986
1987 info->key = (char *)info + sizeof(struct kmod_module_info)
1988 + valuelen + 1;
1989 memcpy(info->key, key, keylen);
1990 info->key[keylen] = '\0';
1991 memcpy(info->value, value, valuelen);
1992 info->value[valuelen] = '\0';
1993 return info;
1994}
1995
1996static void kmod_module_info_free(struct kmod_module_info *info)
1997{
1998 free(info);
1999}
2000
2001/**
2002 * kmod_module_get_info:
2003 * @mod: kmod module
2004 * @list: where to return list of module information. Use
2005 * kmod_module_info_get_key() and
2006 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002007 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002008 *
2009 * Get a list of entries in ELF section ".modinfo", these contain
2010 * alias, license, depends, vermagic and other keys with respective
2011 * values.
2012 *
2013 * After use, free the @list by calling kmod_module_info_free_list().
2014 *
2015 * Returns: 0 on success or < 0 otherwise.
2016 */
2017KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2018{
2019 struct kmod_file *file;
2020 struct kmod_elf *elf;
2021 const char *path;
2022 const void *mem;
2023 char **strings;
2024 size_t size;
2025 int i, count, ret = 0;
2026
2027 if (mod == NULL || list == NULL)
2028 return -ENOENT;
2029
2030 assert(*list == NULL);
2031
2032 path = kmod_module_get_path(mod);
2033 if (path == NULL)
2034 return -ENOENT;
2035
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002036 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002037 if (file == NULL)
2038 return -errno;
2039
2040 size = kmod_file_get_size(file);
2041 mem = kmod_file_get_contents(file);
2042
2043 elf = kmod_elf_new(mem, size);
2044 if (elf == NULL) {
2045 ret = -errno;
2046 goto elf_open_error;
2047 }
2048
2049 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
2050 if (count < 0) {
2051 ret = count;
2052 goto get_strings_error;
2053 }
2054
2055 for (i = 0; i < count; i++) {
2056 struct kmod_module_info *info;
2057 struct kmod_list *n;
2058 const char *key, *value;
2059 size_t keylen, valuelen;
2060
2061 key = strings[i];
2062 value = strchr(key, '=');
2063 if (value == NULL) {
2064 keylen = strlen(key);
2065 valuelen = 0;
2066 } else {
2067 keylen = value - key;
2068 value++;
2069 valuelen = strlen(value);
2070 }
2071
2072 info = kmod_module_info_new(key, keylen, value, valuelen);
2073 if (info == NULL) {
2074 ret = -errno;
2075 kmod_module_info_free_list(*list);
2076 *list = NULL;
2077 goto list_error;
2078 }
2079
2080 n = kmod_list_append(*list, info);
2081 if (n != NULL)
2082 *list = n;
2083 else {
2084 kmod_module_info_free(info);
2085 kmod_module_info_free_list(*list);
2086 *list = NULL;
2087 ret = -ENOMEM;
2088 goto list_error;
2089 }
2090 }
2091 ret = count;
2092
2093list_error:
2094 free(strings);
2095get_strings_error:
2096 kmod_elf_unref(elf);
2097elf_open_error:
2098 kmod_file_unref(file);
2099
2100 return ret;
2101}
2102
2103/**
2104 * kmod_module_info_get_key:
2105 * @entry: a list entry representing a kmod module info
2106 *
2107 * Get the key of a kmod module info.
2108 *
2109 * Returns: the key of this kmod module info on success or NULL on
2110 * failure. The string is owned by the info, do not free it.
2111 */
2112KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2113{
2114 struct kmod_module_info *info;
2115
2116 if (entry == NULL)
2117 return NULL;
2118
2119 info = entry->data;
2120 return info->key;
2121}
2122
2123/**
2124 * kmod_module_info_get_value:
2125 * @entry: a list entry representing a kmod module info
2126 *
2127 * Get the value of a kmod module info.
2128 *
2129 * Returns: the value of this kmod module info on success or NULL on
2130 * failure. The string is owned by the info, do not free it.
2131 */
2132KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2133{
2134 struct kmod_module_info *info;
2135
2136 if (entry == NULL)
2137 return NULL;
2138
2139 info = entry->data;
2140 return info->value;
2141}
2142
2143/**
2144 * kmod_module_info_free_list:
2145 * @list: kmod module info list
2146 *
2147 * Release the resources taken by @list
2148 */
2149KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2150{
2151 while (list) {
2152 kmod_module_info_free(list->data);
2153 list = kmod_list_remove(list);
2154 }
2155}
2156
2157struct kmod_module_version {
2158 uint64_t crc;
2159 char symbol[];
2160};
2161
2162static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2163{
2164 struct kmod_module_version *mv;
2165 size_t symbollen = strlen(symbol) + 1;
2166
2167 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2168 if (mv == NULL)
2169 return NULL;
2170
2171 mv->crc = crc;
2172 memcpy(mv->symbol, symbol, symbollen);
2173 return mv;
2174}
2175
2176static void kmod_module_version_free(struct kmod_module_version *version)
2177{
2178 free(version);
2179}
2180
2181/**
2182 * kmod_module_get_versions:
2183 * @mod: kmod module
2184 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002185 * kmod_module_version_get_symbol() and
2186 * kmod_module_version_get_crc(). Release this list with
2187 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002188 *
2189 * Get a list of entries in ELF section "__versions".
2190 *
2191 * After use, free the @list by calling kmod_module_versions_free_list().
2192 *
2193 * Returns: 0 on success or < 0 otherwise.
2194 */
2195KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2196{
2197 struct kmod_file *file;
2198 struct kmod_elf *elf;
2199 const char *path;
2200 const void *mem;
2201 struct kmod_modversion *versions;
2202 size_t size;
2203 int i, count, ret = 0;
2204
2205 if (mod == NULL || list == NULL)
2206 return -ENOENT;
2207
2208 assert(*list == NULL);
2209
2210 path = kmod_module_get_path(mod);
2211 if (path == NULL)
2212 return -ENOENT;
2213
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002214 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002215 if (file == NULL)
2216 return -errno;
2217
2218 size = kmod_file_get_size(file);
2219 mem = kmod_file_get_contents(file);
2220
2221 elf = kmod_elf_new(mem, size);
2222 if (elf == NULL) {
2223 ret = -errno;
2224 goto elf_open_error;
2225 }
2226
2227 count = kmod_elf_get_modversions(elf, &versions);
2228 if (count < 0) {
2229 ret = count;
2230 goto get_strings_error;
2231 }
2232
2233 for (i = 0; i < count; i++) {
2234 struct kmod_module_version *mv;
2235 struct kmod_list *n;
2236
2237 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2238 if (mv == NULL) {
2239 ret = -errno;
2240 kmod_module_versions_free_list(*list);
2241 *list = NULL;
2242 goto list_error;
2243 }
2244
2245 n = kmod_list_append(*list, mv);
2246 if (n != NULL)
2247 *list = n;
2248 else {
2249 kmod_module_version_free(mv);
2250 kmod_module_versions_free_list(*list);
2251 *list = NULL;
2252 ret = -ENOMEM;
2253 goto list_error;
2254 }
2255 }
2256 ret = count;
2257
2258list_error:
2259 free(versions);
2260get_strings_error:
2261 kmod_elf_unref(elf);
2262elf_open_error:
2263 kmod_file_unref(file);
2264
2265 return ret;
2266}
2267
2268/**
2269 * kmod_module_versions_get_symbol:
2270 * @entry: a list entry representing a kmod module versions
2271 *
2272 * Get the symbol of a kmod module versions.
2273 *
2274 * Returns: the symbol of this kmod module versions on success or NULL
2275 * on failure. The string is owned by the versions, do not free it.
2276 */
2277KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2278{
2279 struct kmod_module_version *version;
2280
2281 if (entry == NULL)
2282 return NULL;
2283
2284 version = entry->data;
2285 return version->symbol;
2286}
2287
2288/**
2289 * kmod_module_version_get_crc:
2290 * @entry: a list entry representing a kmod module version
2291 *
2292 * Get the crc of a kmod module version.
2293 *
2294 * Returns: the crc of this kmod module version on success or NULL on
2295 * failure. The string is owned by the version, do not free it.
2296 */
2297KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2298{
2299 struct kmod_module_version *version;
2300
2301 if (entry == NULL)
2302 return 0;
2303
2304 version = entry->data;
2305 return version->crc;
2306}
2307
2308/**
2309 * kmod_module_versions_free_list:
2310 * @list: kmod module versions list
2311 *
2312 * Release the resources taken by @list
2313 */
2314KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2315{
2316 while (list) {
2317 kmod_module_version_free(list->data);
2318 list = kmod_list_remove(list);
2319 }
2320}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002321
2322struct kmod_module_symbol {
2323 uint64_t crc;
2324 char symbol[];
2325};
2326
2327static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2328{
2329 struct kmod_module_symbol *mv;
2330 size_t symbollen = strlen(symbol) + 1;
2331
2332 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2333 if (mv == NULL)
2334 return NULL;
2335
2336 mv->crc = crc;
2337 memcpy(mv->symbol, symbol, symbollen);
2338 return mv;
2339}
2340
2341static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2342{
2343 free(symbol);
2344}
2345
2346/**
2347 * kmod_module_get_symbols:
2348 * @mod: kmod module
2349 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002350 * kmod_module_symbol_get_symbol() and
2351 * kmod_module_symbol_get_crc(). Release this list with
2352 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002353 *
2354 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2355 *
2356 * After use, free the @list by calling kmod_module_symbols_free_list().
2357 *
2358 * Returns: 0 on success or < 0 otherwise.
2359 */
2360KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2361{
2362 struct kmod_file *file;
2363 struct kmod_elf *elf;
2364 const char *path;
2365 const void *mem;
2366 struct kmod_modversion *symbols;
2367 size_t size;
2368 int i, count, ret = 0;
2369
2370 if (mod == NULL || list == NULL)
2371 return -ENOENT;
2372
2373 assert(*list == NULL);
2374
2375 path = kmod_module_get_path(mod);
2376 if (path == NULL)
2377 return -ENOENT;
2378
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002379 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002380 if (file == NULL)
2381 return -errno;
2382
2383 size = kmod_file_get_size(file);
2384 mem = kmod_file_get_contents(file);
2385
2386 elf = kmod_elf_new(mem, size);
2387 if (elf == NULL) {
2388 ret = -errno;
2389 goto elf_open_error;
2390 }
2391
2392 count = kmod_elf_get_symbols(elf, &symbols);
2393 if (count < 0) {
2394 ret = count;
2395 goto get_strings_error;
2396 }
2397
2398 for (i = 0; i < count; i++) {
2399 struct kmod_module_symbol *mv;
2400 struct kmod_list *n;
2401
2402 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2403 if (mv == NULL) {
2404 ret = -errno;
2405 kmod_module_symbols_free_list(*list);
2406 *list = NULL;
2407 goto list_error;
2408 }
2409
2410 n = kmod_list_append(*list, mv);
2411 if (n != NULL)
2412 *list = n;
2413 else {
2414 kmod_module_symbol_free(mv);
2415 kmod_module_symbols_free_list(*list);
2416 *list = NULL;
2417 ret = -ENOMEM;
2418 goto list_error;
2419 }
2420 }
2421 ret = count;
2422
2423list_error:
2424 free(symbols);
2425get_strings_error:
2426 kmod_elf_unref(elf);
2427elf_open_error:
2428 kmod_file_unref(file);
2429
2430 return ret;
2431}
2432
2433/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002434 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002435 * @entry: a list entry representing a kmod module symbols
2436 *
2437 * Get the symbol of a kmod module symbols.
2438 *
2439 * Returns: the symbol of this kmod module symbols on success or NULL
2440 * on failure. The string is owned by the symbols, do not free it.
2441 */
2442KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2443{
2444 struct kmod_module_symbol *symbol;
2445
2446 if (entry == NULL)
2447 return NULL;
2448
2449 symbol = entry->data;
2450 return symbol->symbol;
2451}
2452
2453/**
2454 * kmod_module_symbol_get_crc:
2455 * @entry: a list entry representing a kmod module symbol
2456 *
2457 * Get the crc of a kmod module symbol.
2458 *
2459 * Returns: the crc of this kmod module symbol on success or NULL on
2460 * failure. The string is owned by the symbol, do not free it.
2461 */
2462KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2463{
2464 struct kmod_module_symbol *symbol;
2465
2466 if (entry == NULL)
2467 return 0;
2468
2469 symbol = entry->data;
2470 return symbol->crc;
2471}
2472
2473/**
2474 * kmod_module_symbols_free_list:
2475 * @list: kmod module symbols list
2476 *
2477 * Release the resources taken by @list
2478 */
2479KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2480{
2481 while (list) {
2482 kmod_module_symbol_free(list->data);
2483 list = kmod_list_remove(list);
2484 }
2485}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002486
2487struct kmod_module_dependency_symbol {
2488 uint64_t crc;
2489 uint8_t bind;
2490 char symbol[];
2491};
2492
2493static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2494{
2495 struct kmod_module_dependency_symbol *mv;
2496 size_t symbollen = strlen(symbol) + 1;
2497
2498 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2499 if (mv == NULL)
2500 return NULL;
2501
2502 mv->crc = crc;
2503 mv->bind = bind;
2504 memcpy(mv->symbol, symbol, symbollen);
2505 return mv;
2506}
2507
2508static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2509{
2510 free(dependency_symbol);
2511}
2512
2513/**
2514 * kmod_module_get_dependency_symbols:
2515 * @mod: kmod module
2516 * @list: where to return list of module dependency_symbols. Use
2517 * kmod_module_dependency_symbol_get_symbol() and
2518 * kmod_module_dependency_symbol_get_crc(). Release this list with
2519 * kmod_module_dependency_symbols_free_list()
2520 *
2521 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2522 *
2523 * After use, free the @list by calling
2524 * kmod_module_dependency_symbols_free_list().
2525 *
2526 * Returns: 0 on success or < 0 otherwise.
2527 */
2528KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2529{
2530 struct kmod_file *file;
2531 struct kmod_elf *elf;
2532 const char *path;
2533 const void *mem;
2534 struct kmod_modversion *symbols;
2535 size_t size;
2536 int i, count, ret = 0;
2537
2538 if (mod == NULL || list == NULL)
2539 return -ENOENT;
2540
2541 assert(*list == NULL);
2542
2543 path = kmod_module_get_path(mod);
2544 if (path == NULL)
2545 return -ENOENT;
2546
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002547 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002548 if (file == NULL)
2549 return -errno;
2550
2551 size = kmod_file_get_size(file);
2552 mem = kmod_file_get_contents(file);
2553
2554 elf = kmod_elf_new(mem, size);
2555 if (elf == NULL) {
2556 ret = -errno;
2557 goto elf_open_error;
2558 }
2559
2560 count = kmod_elf_get_dependency_symbols(elf, &symbols);
2561 if (count < 0) {
2562 ret = count;
2563 goto get_strings_error;
2564 }
2565
2566 for (i = 0; i < count; i++) {
2567 struct kmod_module_dependency_symbol *mv;
2568 struct kmod_list *n;
2569
2570 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2571 symbols[i].bind,
2572 symbols[i].symbol);
2573 if (mv == NULL) {
2574 ret = -errno;
2575 kmod_module_dependency_symbols_free_list(*list);
2576 *list = NULL;
2577 goto list_error;
2578 }
2579
2580 n = kmod_list_append(*list, mv);
2581 if (n != NULL)
2582 *list = n;
2583 else {
2584 kmod_module_dependency_symbol_free(mv);
2585 kmod_module_dependency_symbols_free_list(*list);
2586 *list = NULL;
2587 ret = -ENOMEM;
2588 goto list_error;
2589 }
2590 }
2591 ret = count;
2592
2593list_error:
2594 free(symbols);
2595get_strings_error:
2596 kmod_elf_unref(elf);
2597elf_open_error:
2598 kmod_file_unref(file);
2599
2600 return ret;
2601}
2602
2603/**
2604 * kmod_module_dependency_symbol_get_symbol:
2605 * @entry: a list entry representing a kmod module dependency_symbols
2606 *
2607 * Get the dependency symbol of a kmod module
2608 *
2609 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2610 * on failure. The string is owned by the dependency_symbols, do not free it.
2611 */
2612KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2613{
2614 struct kmod_module_dependency_symbol *dependency_symbol;
2615
2616 if (entry == NULL)
2617 return NULL;
2618
2619 dependency_symbol = entry->data;
2620 return dependency_symbol->symbol;
2621}
2622
2623/**
2624 * kmod_module_dependency_symbol_get_crc:
2625 * @entry: a list entry representing a kmod module dependency_symbol
2626 *
2627 * Get the crc of a kmod module dependency_symbol.
2628 *
2629 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2630 * failure. The string is owned by the dependency_symbol, do not free it.
2631 */
2632KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2633{
2634 struct kmod_module_dependency_symbol *dependency_symbol;
2635
2636 if (entry == NULL)
2637 return 0;
2638
2639 dependency_symbol = entry->data;
2640 return dependency_symbol->crc;
2641}
2642
2643/**
2644 * kmod_module_dependency_symbol_get_bind:
2645 * @entry: a list entry representing a kmod module dependency_symbol
2646 *
2647 * Get the bind type of a kmod module dependency_symbol.
2648 *
2649 * Returns: the bind of this kmod module dependency_symbol on success
2650 * or < 0 on failure.
2651 */
2652KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2653{
2654 struct kmod_module_dependency_symbol *dependency_symbol;
2655
2656 if (entry == NULL)
2657 return 0;
2658
2659 dependency_symbol = entry->data;
2660 return dependency_symbol->bind;
2661}
2662
2663/**
2664 * kmod_module_dependency_symbols_free_list:
2665 * @list: kmod module dependency_symbols list
2666 *
2667 * Release the resources taken by @list
2668 */
2669KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2670{
2671 while (list) {
2672 kmod_module_dependency_symbol_free(list->data);
2673 list = kmod_list_remove(list);
2674 }
2675}