blob: d6d081092a62c3582f45abe7d75313b2d2155d38 [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
Lucas De Marchi671d4892011-12-05 20:23:05 -0200101int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200102{
103 struct kmod_ctx *ctx = mod->ctx;
104 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200105 const char *dirname;
106 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200107 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200108 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200109 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200110
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200111 if (mod->init.dep)
112 return mod->n_dep;
113 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200114 mod->init.dep = true;
115
116 p = strchr(line, ':');
117 if (p == NULL)
118 return 0;
119
Lucas De Marchi671d4892011-12-05 20:23:05 -0200120 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200121 dirname = kmod_get_dirname(mod->ctx);
122 dirnamelen = strlen(dirname);
123 if (dirnamelen + 2 >= PATH_MAX)
124 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200125
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200126 memcpy(buf, dirname, dirnamelen);
127 buf[dirnamelen] = '/';
128 dirnamelen++;
129 buf[dirnamelen] = '\0';
130
131 if (mod->path == NULL) {
132 const char *str = path_join(line, dirnamelen, buf);
133 if (str == NULL)
134 return 0;
135 mod->path = strdup(str);
136 if (mod->path == NULL)
137 return 0;
138 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200139
Lucas De Marchi7636e722011-12-01 17:56:03 -0200140 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200141 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
142 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200143 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200144 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200145
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200146 path = path_join(p, dirnamelen, buf);
147 if (path == NULL) {
148 ERR(ctx, "could not join path '%s' and '%s'.\n",
149 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200150 goto fail;
151 }
152
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200153 err = kmod_module_new_from_path(ctx, path, &depmod);
154 if (err < 0) {
155 ERR(ctx, "ctx=%p path=%s error=%s\n",
156 ctx, path, strerror(-err));
157 goto fail;
158 }
159
160 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200161
Lucas De Marchib94a7372011-12-26 20:10:49 -0200162 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200163 n++;
164 }
165
166 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
167
168 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200169 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200170 return n;
171
172fail:
173 kmod_module_unref_list(list);
174 mod->init.dep = false;
175 return err;
176}
177
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200178void kmod_module_set_visited(struct kmod_module *mod, bool visited)
179{
180 mod->visited = visited;
181}
182
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200183/*
184 * Memory layout with alias:
185 *
186 * struct kmod_module {
187 * hashkey -----.
188 * alias -----. |
189 * name ----. | |
190 * } | | |
191 * name <----------' | |
192 * alias <-----------' |
193 * name\alias <--------'
194 *
195 * Memory layout without alias:
196 *
197 * struct kmod_module {
198 * hashkey ---.
199 * alias -----|----> NULL
200 * name ----. |
201 * } | |
202 * name <----------'-'
203 *
204 * @key is "name\alias" or "name" (in which case alias == NULL)
205 */
206static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
207 const char *name, size_t namelen,
208 const char *alias, size_t aliaslen,
209 struct kmod_module **mod)
210{
211 struct kmod_module *m;
212 size_t keylen;
213
214 m = kmod_pool_get_module(ctx, key);
215 if (m != NULL) {
216 *mod = kmod_module_ref(m);
217 return 0;
218 }
219
220 if (alias == NULL)
221 keylen = namelen;
222 else
223 keylen = namelen + aliaslen + 1;
224
225 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
226 if (m == NULL) {
227 free(m);
228 return -ENOMEM;
229 }
230
231 memset(m, 0, sizeof(*m));
232
233 m->ctx = kmod_ref(ctx);
234 m->name = (char *)m + sizeof(*m);
235 memcpy(m->name, key, keylen + 1);
236 if (alias == NULL) {
237 m->hashkey = m->name;
238 m->alias = NULL;
239 } else {
240 m->name[namelen] = '\0';
241 m->alias = m->name + namelen + 1;
242 m->hashkey = m->name + keylen + 1;
243 memcpy(m->hashkey, key, keylen + 1);
244 }
245
246 m->refcount = 1;
247 kmod_pool_add_module(ctx, m, m->hashkey);
248 *mod = m;
249
250 return 0;
251}
252
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200253/**
254 * kmod_module_new_from_name:
255 * @ctx: kmod library context
256 * @name: name of the module
257 * @mod: where to save the created struct kmod_module
258 *
259 * Create a new struct kmod_module using the module name. @name can not be an
260 * alias, file name or anything else; it must be a module name. There's no
261 * check if the module does exists in the system.
262 *
263 * This function is also used internally by many others that return a new
264 * struct kmod_module or a new list of modules.
265 *
266 * The initial refcount is 1, and needs to be decremented to release the
267 * resources of the kmod_module. Since libkmod keeps track of all
268 * kmod_modules created, they are all released upon @ctx destruction too. Do
269 * not unref @ctx before all the desired operations with the returned
270 * kmod_module are done.
271 *
272 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
273 * module name or if memory allocation failed.
274 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200275KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
276 const char *name,
277 struct kmod_module **mod)
278{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200279 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200280 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200281
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200282 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200283 return -ENOENT;
284
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200285 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200286
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200287 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200288}
289
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200290int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
291 const char *name, struct kmod_module **mod)
292{
293 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200294 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200295 size_t namelen = strlen(name);
296 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200297
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200298 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200299 return -ENAMETOOLONG;
300
301 memcpy(key, name, namelen);
302 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200303 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200304
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200305 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200306 if (err < 0)
307 return err;
308
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200309 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200310}
311
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200312/**
313 * kmod_module_new_from_path:
314 * @ctx: kmod library context
315 * @path: path where to find the given module
316 * @mod: where to save the created struct kmod_module
317 *
318 * Create a new struct kmod_module using the module path. @path must be an
319 * existent file with in the filesystem and must be accessible to libkmod.
320 *
321 * The initial refcount is 1, and needs to be decremented to release the
322 * resources of the kmod_module. Since libkmod keeps track of all
323 * kmod_modules created, they are all released upon @ctx destruction too. Do
324 * not unref @ctx before all the desired operations with the returned
325 * kmod_module are done.
326 *
327 * If @path is relative, it's treated as relative to the current working
328 * directory. Otherwise, give an absolute path.
329 *
330 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
331 * it's not a valid file for a kmod_module or if memory allocation failed.
332 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200333KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
334 const char *path,
335 struct kmod_module **mod)
336{
337 struct kmod_module *m;
338 int err;
339 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200340 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200341 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200342 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200343
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200344 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200345 return -ENOENT;
346
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200347 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200348 if (abspath == NULL) {
349 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200350 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200351 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200352
353 err = stat(abspath, &st);
354 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200355 err = -errno;
356 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200357 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200358 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200359 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200360
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200361 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200362 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200363 free(abspath);
364 return -ENOENT;
365 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200366
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200367 m = kmod_pool_get_module(ctx, name);
368 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200369 if (m->path == NULL)
370 m->path = abspath;
371 else if (streq(m->path, abspath))
372 free(abspath);
373 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200374 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 -0200375 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200376 free(abspath);
377 return -EEXIST;
378 }
379
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200380 *mod = kmod_module_ref(m);
381 return 0;
382 }
383
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200384 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
385 if (err < 0)
386 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200387
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200388 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200389 *mod = m;
390
391 return 0;
392}
393
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200394/**
395 * kmod_module_unref:
396 * @mod: kmod module
397 *
398 * Drop a reference of the kmod module. If the refcount reaches zero, its
399 * resources are released.
400 *
401 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
402 * returns the passed @mod with its refcount decremented.
403 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200404KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
405{
406 if (mod == NULL)
407 return NULL;
408
409 if (--mod->refcount > 0)
410 return mod;
411
412 DBG(mod->ctx, "kmod_module %p released\n", mod);
413
Lucas De Marchi4084c172011-12-15 13:43:22 -0200414 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200415 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200416 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200417 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200418 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200419 free(mod);
420 return NULL;
421}
422
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200423/**
424 * kmod_module_ref:
425 * @mod: kmod module
426 *
427 * Take a reference of the kmod module, incrementing its refcount.
428 *
429 * Returns: the passed @module with its refcount incremented.
430 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200431KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
432{
433 if (mod == NULL)
434 return NULL;
435
436 mod->refcount++;
437
438 return mod;
439}
440
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200441#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
442 do { \
443 if ((_err) < 0) \
444 goto _label_err; \
445 if (*(_list) != NULL) \
446 goto finish; \
447 } while (0)
448
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200449/**
450 * kmod_module_new_from_lookup:
451 * @ctx: kmod library context
452 * @given_alias: alias to look for
453 * @list: an empty list where to save the list of modules matching
454 * @given_alias
455 *
456 * Create a new list of kmod modules using an alias or module name and lookup
457 * libkmod's configuration files and indexes in order to find the module.
458 * Once it's found in one of the places, it stops searching and create the
459 * list of modules that is saved in @list.
460 *
461 * The search order is: 1. aliases in configuration file; 2. module names in
462 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
463 * in modules.alias index.
464 *
465 * The initial refcount is 1, and needs to be decremented to release the
466 * resources of the kmod_module. The returned @list must be released by
467 * calling kmod_module_unref_list(). Since libkmod keeps track of all
468 * kmod_modules created, they are all released upon @ctx destruction too. Do
469 * not unref @ctx before all the desired operations with the returned list are
470 * completed.
471 *
472 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
473 * methods failed, which is basically due to memory allocation fail. If module
474 * is not found, it still returns 0, but @list is an empty list.
475 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200476KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200477 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200478 struct kmod_list **list)
479{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200480 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200481 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200482
Lucas De Marchi4308b172011-12-13 10:26:04 -0200483 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200484 return -ENOENT;
485
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200486 if (list == NULL || *list != NULL) {
487 ERR(ctx, "An empty list is needed to create lookup\n");
488 return -ENOSYS;
489 }
490
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200491 if (alias_normalize(given_alias, alias, NULL) < 0) {
492 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200493 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200494 }
495
496 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200497
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200498 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200499 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200500 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200501
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200502 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200503 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
504 CHECK_ERR_AND_FINISH(err, fail, list, finish);
505
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200506 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200507 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
508 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200509
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200510 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200511 err = kmod_lookup_alias_from_commands(ctx, alias, list);
512 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200513
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200514 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200515 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
516 CHECK_ERR_AND_FINISH(err, fail, list, finish);
517
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200518finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200519 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200520 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200521fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200522 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200523 kmod_module_unref_list(*list);
524 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200525 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200526}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200527#undef CHECK_ERR_AND_FINISH
528
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200529/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200530 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200531 * @list: list of kmod modules
532 *
533 * Drop a reference of each kmod module in @list and releases the resources
534 * taken by the list itself.
535 *
536 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
537 * returns the passed @mod with its refcount decremented.
538 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200539KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
540{
541 for (; list != NULL; list = kmod_list_remove(list))
542 kmod_module_unref(list->data);
543
544 return 0;
545}
546
Lucas De Marchi0d467432011-12-31 11:15:52 -0200547/**
548 * kmod_module_get_filtered_blacklist:
549 * @ctx: kmod library context
550 * @input: list of kmod_module to be filtered with blacklist
551 * @output: where to save the new list
552 *
553 * Given a list @input, this function filter it out with config's blacklist
554 * ans save it in @output.
555 *
556 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
557 * list.
558 */
559KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
560 const struct kmod_list *input,
561 struct kmod_list **output)
562{
563 const struct kmod_list *li;
564 const struct kmod_list *blacklist;
565
566 if (ctx == NULL || output == NULL)
567 return -ENOENT;
568
569 *output = NULL;
570 if (input == NULL)
571 return 0;
572
573 blacklist = kmod_get_blacklists(ctx);
574 kmod_list_foreach(li, input) {
575 struct kmod_module *mod = li->data;
576 const struct kmod_list *lb;
577 struct kmod_list *node;
578 bool filtered = false;
579
580 kmod_list_foreach(lb, blacklist) {
581 const char *name = lb->data;
582
Lucas De Marchi4926cb52011-12-31 11:21:52 -0200583 if (streq(name, mod->name)) {
Lucas De Marchi0d467432011-12-31 11:15:52 -0200584 filtered = true;
585 break;
586 }
587 }
588
589 if (filtered)
590 continue;
591
592 node = kmod_list_append(*output, mod);
593 if (node == NULL)
594 goto fail;
595
596 *output = node;
597 kmod_module_ref(mod);
598 }
599
600 return 0;
601
602fail:
603 kmod_module_unref_list(*output);
604 *output = NULL;
605 return -ENOMEM;
606}
607
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200608static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
609{
610 if (!mod->init.dep) {
611 /* lazy init */
612 char *line = kmod_search_moddep(mod->ctx, mod->name);
613
614 if (line == NULL)
615 return NULL;
616
617 kmod_module_parse_depline((struct kmod_module *)mod, line);
618 free(line);
619
620 if (!mod->init.dep)
621 return NULL;
622 }
623
624 return mod->dep;
625}
626
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200627/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200628 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200629 * @mod: kmod module
630 *
631 * Search the modules.dep index to find the dependencies of the given @mod.
632 * The result is cached in @mod, so subsequent calls to this function will
633 * return the already searched list of modules.
634 *
635 * Returns: NULL on failure or if there are any dependencies. Otherwise it
636 * returns a list of kmod modules that can be released by calling
637 * kmod_module_unref_list().
638 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200639KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200640{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200641 struct kmod_list *l, *l_new, *list_new = NULL;
642
643 if (mod == NULL)
644 return NULL;
645
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200646 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200647
648 kmod_list_foreach(l, mod->dep) {
649 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
650 if (l_new == NULL) {
651 kmod_module_unref(l->data);
652 goto fail;
653 }
654
655 list_new = l_new;
656 }
657
658 return list_new;
659
660fail:
661 ERR(mod->ctx, "out of memory\n");
662 kmod_module_unref_list(list_new);
663 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200664}
665
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200666/**
667 * kmod_module_get_module:
668 * @entry: an entry in a list of kmod modules.
669 *
670 * Get the kmod module of this @entry in the list, increasing its refcount.
671 * After it's used, unref it. Since the refcount is incremented upon return,
672 * you still have to call kmod_module_unref_list() to release the list of kmod
673 * modules.
674 *
675 * Returns: NULL on failure or the kmod_module contained in this list entry
676 * with its refcount incremented.
677 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200678KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200679{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200680 if (entry == NULL)
681 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200682
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200683 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200684}
685
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200686/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200687 * kmod_module_get_name:
688 * @mod: kmod module
689 *
690 * Get the name of this kmod module. Name is always available, independently
691 * if it was created by kmod_module_new_from_name() or another function and
692 * it's always normalized (dashes are replaced with underscores).
693 *
694 * Returns: the name of this kmod module.
695 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200696KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200697{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200698 if (mod == NULL)
699 return NULL;
700
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200701 return mod->name;
702}
703
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200704/**
705 * kmod_module_get_path:
706 * @mod: kmod module
707 *
708 * Get the path of this kmod module. If this kmod module was not created by
709 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200710 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200711 *
712 * Returns: the path of this kmod module or NULL if such information is not
713 * available.
714 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200715KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200716{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200717 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200718
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200719 if (mod == NULL)
720 return NULL;
721
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200722 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200723
Lucas De Marchie005fac2011-12-08 10:42:34 -0200724 if (mod->path != NULL)
725 return mod->path;
726 if (mod->init.dep)
727 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200728
Lucas De Marchie005fac2011-12-08 10:42:34 -0200729 /* lazy init */
730 line = kmod_search_moddep(mod->ctx, mod->name);
731 if (line == NULL)
732 return NULL;
733
734 kmod_module_parse_depline((struct kmod_module *) mod, line);
735 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200736
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200737 return mod->path;
738}
739
740
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200741extern long delete_module(const char *name, unsigned int flags);
742
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200743/**
744 * kmod_module_remove_module:
745 * @mod: kmod module
746 * @flags: flags to pass to Linux kernel when removing the module
747 *
748 * Remove a module from Linux kernel.
749 *
750 * Returns: 0 on success or < 0 on failure.
751 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200752KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
753 unsigned int flags)
754{
755 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200756
757 if (mod == NULL)
758 return -ENOENT;
759
760 /* Filter out other flags */
761 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
762
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200763 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200764 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200765 err = -errno;
766 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200767 }
768
Lucas De Marchiba998b92012-01-11 00:08:14 -0200769 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200770}
771
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200772extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200773
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200774/**
775 * kmod_module_insert_module:
776 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200777 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
778 * behavior of this function.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200779 * @options: module's options to pass to Linux Kernel.
780 *
781 * Insert a module in Linux kernel. It opens the file pointed by @mod,
782 * mmap'ing it and passing to kernel.
783 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200784 * Returns: 0 on success or < 0 on failure. If module is already loaded it
785 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200786 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200787KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200788 unsigned int flags,
789 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200790{
791 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200792 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200793 off_t size;
794 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200795 struct kmod_elf *elf = NULL;
796 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200797 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200798
799 if (mod == NULL)
800 return -ENOENT;
801
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200802 path = kmod_module_get_path(mod);
803 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500804 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200805 return -ENOSYS;
806 }
807
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200808 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200809 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200810 err = -errno;
811 return err;
812 }
813
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200814 size = kmod_file_get_size(file);
815 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200816
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200817 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
818 elf = kmod_elf_new(mem, size);
819 if (elf == NULL) {
820 err = -errno;
821 goto elf_failed;
822 }
823
824 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
825 err = kmod_elf_strip_section(elf, "__versions");
826 if (err < 0)
827 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
828 }
829
830 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
831 err = kmod_elf_strip_vermagic(elf);
832 if (err < 0)
833 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
834 }
835
836 mem = kmod_elf_get_memory(elf);
837 }
838
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200839 err = init_module(mem, size, args);
Lucas De Marchibbf59322011-12-30 14:13:33 -0200840 if (err < 0) {
841 err = -errno;
842 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
843 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200844
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200845 if (elf != NULL)
846 kmod_elf_unref(elf);
847elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200848 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200849
850 return err;
851}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200852
Lucas De Marchiddbda022011-12-27 11:40:10 -0200853static bool module_is_blacklisted(struct kmod_module *mod)
854{
855 struct kmod_ctx *ctx = mod->ctx;
856 const struct kmod_list *bl = kmod_get_blacklists(ctx);
857 const struct kmod_list *l;
858
859 kmod_list_foreach(l, bl) {
860 const char *modname = kmod_blacklist_get_modname(l);
861
862 if (streq(modname, mod->name))
863 return true;
864 }
865
866 return false;
867}
868
Lucas De Marchiddbda022011-12-27 11:40:10 -0200869static int command_do(struct kmod_module *mod, const char *type,
870 const char *cmd)
871{
872 const char *modname = kmod_module_get_name(mod);
873 int err;
874
875 DBG(mod->ctx, "%s %s\n", type, cmd);
876
877 setenv("MODPROBE_MODULE", modname, 1);
878 err = system(cmd);
879 unsetenv("MODPROBE_MODULE");
880
881 if (err == -1 || WEXITSTATUS(err)) {
882 ERR(mod->ctx, "Error running %s command for %s\n",
883 type, modname);
884 if (err != -1)
885 err = -WEXITSTATUS(err);
886 }
887
888 return err;
889}
890
Lucas De Marchib1a51252012-01-29 01:49:09 -0200891struct probe_insert_cb {
892 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
893 void *data;
894};
895
Lucas De Marchiddbda022011-12-27 11:40:10 -0200896static int module_do_install_commands(struct kmod_module *mod,
897 const char *options,
898 struct probe_insert_cb *cb)
899{
900 const char *command = kmod_module_get_install_commands(mod);
901 char *p, *cmd;
902 int err;
903 size_t cmdlen, options_len, varlen;
904
905 assert(command);
906
907 if (options == NULL)
908 options = "";
909
910 options_len = strlen(options);
911 cmdlen = strlen(command);
912 varlen = sizeof("$CMDLINE_OPTS") - 1;
913
914 cmd = memdup(command, cmdlen + 1);
915 if (cmd == NULL)
916 return -ENOMEM;
917
918 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
919 size_t prefixlen = p - cmd;
920 size_t suffixlen = cmdlen - prefixlen - varlen;
921 size_t slen = cmdlen - varlen + options_len;
922 char *suffix = p + varlen;
923 char *s = malloc(slen + 1);
924 if (s == NULL) {
925 free(cmd);
926 return -ENOMEM;
927 }
928 memcpy(s, cmd, p - cmd);
929 memcpy(s + prefixlen, options, options_len);
930 memcpy(s + prefixlen + options_len, suffix, suffixlen);
931 s[slen] = '\0';
932
933 free(cmd);
934 cmd = s;
935 cmdlen = slen;
936 }
937
938 if (cb->run_install != NULL)
939 err = cb->run_install(mod, cmd, cb->data);
940 else
941 err = command_do(mod, "install", cmd);
942
943 free(cmd);
944
945 return err;
946}
947
Lucas De Marchiddbda022011-12-27 11:40:10 -0200948static char *module_options_concat(const char *opt, const char *xopt)
949{
950 // TODO: we might need to check if xopt overrides options on opt
951 size_t optlen = opt == NULL ? 0 : strlen(opt);
952 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
953 char *r;
954
955 if (optlen == 0 && xoptlen == 0)
956 return NULL;
957
958 r = malloc(optlen + xoptlen + 2);
959
960 if (opt != NULL) {
961 memcpy(r, opt, optlen);
962 r[optlen] = ' ';
963 optlen++;
964 }
965
966 if (xopt != NULL)
967 memcpy(r + optlen, xopt, xoptlen);
968
969 r[optlen + xoptlen] = '\0';
970
971 return r;
972}
973
Lucas De Marchib1a51252012-01-29 01:49:09 -0200974static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -0200975 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -0200976 struct kmod_list **list);
977
978/* re-entrant */
979static int __kmod_module_fill_softdep(struct kmod_module *mod,
980 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -0200981{
Lucas De Marchib1a51252012-01-29 01:49:09 -0200982 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200983 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200984
985 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -0200986 if (err < 0) {
987 ERR(mod->ctx, "could not get softdep: %s", strerror(-err));
988 goto fail;
989 }
Lucas De Marchiddbda022011-12-27 11:40:10 -0200990
Lucas De Marchib1a51252012-01-29 01:49:09 -0200991 kmod_list_foreach(l, pre) {
992 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -0200993 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -0200994 if (err < 0)
995 goto fail;
996 }
Lucas De Marchiddbda022011-12-27 11:40:10 -0200997
Lucas De Marchib1a51252012-01-29 01:49:09 -0200998 l = kmod_list_append(*list, kmod_module_ref(mod));
999 if (l == NULL) {
1000 kmod_module_unref(mod);
1001 err = -ENOMEM;
1002 goto fail;
1003 }
1004 *list = l;
1005 mod->visited = true;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001006 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001007
Lucas De Marchib1a51252012-01-29 01:49:09 -02001008 kmod_list_foreach(l, post) {
1009 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001010 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001011 if (err < 0)
1012 goto fail;
1013 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001014
Lucas De Marchib1a51252012-01-29 01:49:09 -02001015fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001016 kmod_module_unref_list(pre);
1017 kmod_module_unref_list(post);
1018
1019 return err;
1020}
1021
Lucas De Marchib1a51252012-01-29 01:49:09 -02001022/* re-entrant */
1023static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001024 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001025 struct kmod_list **list)
1026{
1027 struct kmod_list *dep, *l;
1028 int err = 0;
1029
1030 if (mod->visited) {
1031 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1032 mod->name);
1033 return 0;
1034 }
1035
1036 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001037 kmod_list_foreach(l, dep) {
1038 struct kmod_module *m = l->data;
1039 err = __kmod_module_fill_softdep(m, list);
1040 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001041 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001042 }
1043
Lucas De Marchi89e92482012-01-29 02:35:46 -02001044 if (ignorecmd) {
1045 l = kmod_list_append(*list, kmod_module_ref(mod));
1046 if (l == NULL) {
1047 kmod_module_unref(mod);
1048 err = -ENOMEM;
1049 goto finish;
1050 }
1051 *list = l;
1052 mod->ignorecmd = true;
1053 } else
1054 err = __kmod_module_fill_softdep(mod, list);
1055
Lucas De Marchib1a51252012-01-29 01:49:09 -02001056finish:
1057 kmod_module_unref_list(dep);
1058 return err;
1059}
1060
1061static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001062 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001063 struct kmod_list **list)
1064{
1065 int err;
1066
1067 assert(mod != NULL);
1068 assert(list != NULL && *list == NULL);
1069
1070 /*
1071 * Make sure we don't get screwed by previous calls to this function
1072 */
1073 kmod_set_modules_visited(mod->ctx, false);
1074
Lucas De Marchi89e92482012-01-29 02:35:46 -02001075 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001076 if (err < 0) {
1077 kmod_module_unref_list(*list);
1078 *list = NULL;
1079 }
1080
1081 return err;
1082}
1083
Lucas De Marchiddbda022011-12-27 11:40:10 -02001084/**
1085 * kmod_module_probe_insert_module:
1086 * @mod: kmod module
1087 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1088 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001089 * @extra_options: module's options to pass to Linux Kernel. It applies only
1090 * to @mod, not to its dependencies.
1091 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001092 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001093 * @print_action: function to call with the action being taken (install or
1094 * insmod). It's useful for tools like modprobe when running with verbose
1095 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001096 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001097 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001098 * install commands and applying blacklist.
1099 *
1100 * If @run_install is NULL, and the flag KMOD_PROBE_STOP_ON_COMMANDS is not
Lucas De Marchib1a51252012-01-29 01:49:09 -02001101 * given, this function will fork and exec by calling system(3). Don't pass a
1102 * NULL argument in @run_install if your binary is setuid/setgid (see warning
1103 * in system(3)). If you need control over the execution of an install
1104 * command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001105 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001106 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1107 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001108 */
1109KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1110 unsigned int flags, const char *extra_options,
1111 int (*run_install)(struct kmod_module *m,
1112 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001113 const void *data,
1114 void (*print_action)(struct kmod_module *m,
1115 bool install,
1116 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001117{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001118 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001119 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001120 int err;
1121
1122 if (mod == NULL)
1123 return -ENOENT;
1124
1125 err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
1126 KMOD_PROBE_APPLY_BLACKLIST_ALL);
1127 if (err != 0) {
1128 if (module_is_blacklisted(mod))
1129 return err;
1130 }
1131
Lucas De Marchi89e92482012-01-29 02:35:46 -02001132 err = kmod_module_get_probe_list(mod,
1133 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001134 if (err < 0)
1135 return err;
1136
1137 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1138 struct kmod_list *filtered = NULL;
1139
1140 err = kmod_module_get_filtered_blacklist(mod->ctx,
1141 list, &filtered);
1142 if (err < 0)
1143 return err;
1144
1145 kmod_module_unref_list(list);
1146 if (filtered == NULL)
1147 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1148
1149 list = filtered;
1150 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001151
1152 cb.run_install = run_install;
1153 cb.data = (void *) data;
1154
Lucas De Marchib1a51252012-01-29 01:49:09 -02001155 kmod_list_foreach(l, list) {
1156 struct kmod_module *m = l->data;
1157 const char *moptions = kmod_module_get_options(m);
1158 const char *cmd = kmod_module_get_install_commands(m);
1159 char *options = module_options_concat(moptions,
1160 m == mod ? extra_options : NULL);
1161
Lucas De Marchi89e92482012-01-29 02:35:46 -02001162 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001163 if (flags & KMOD_PROBE_STOP_ON_COMMAND) {
1164 DBG(mod->ctx, "Stopping on '%s': "
1165 "install command\n", m->name);
1166 err = KMOD_PROBE_STOP_ON_COMMAND;
1167 free(options);
1168 break;
1169 }
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001170
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001171 if (print_action != NULL)
1172 print_action(m, true, options ?: "");
1173
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001174 if (!(flags & KMOD_PROBE_DRY_RUN))
1175 err = module_do_install_commands(m, options,
1176 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001177 } else {
Lucas De Marchi7c10c692012-01-30 18:54:45 -02001178 int state;
1179
1180 if (flags & KMOD_PROBE_IGNORE_LOADED)
1181 state = -1;
1182 else
1183 state = kmod_module_get_initstate(m);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001184
1185 if (state == KMOD_MODULE_LIVE ||
1186 state == KMOD_MODULE_COMING ||
1187 state == KMOD_MODULE_BUILTIN) {
Lucas De Marchi5f351472012-01-30 16:26:52 -02001188 if (m == mod && (flags & KMOD_PROBE_STOP_ON_ALREADY_LOADED)) {
1189 err = KMOD_PROBE_STOP_ON_ALREADY_LOADED;
1190 break;
1191 }
1192
Lucas De Marchi7c10c692012-01-30 18:54:45 -02001193 DBG(mod->ctx, "Ignoring module '%s': "
1194 "already loaded\n", m->name);
1195 err = 0;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001196 }
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001197 if (print_action != NULL)
1198 print_action(m, false, options ?: "");
1199
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001200 if (!(flags & KMOD_PROBE_DRY_RUN))
1201 err = kmod_module_insert_module(m, flags,
1202 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001203 }
1204
1205 free(options);
1206
1207 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001208 * Treat "already loaded" error. If we were told to stop on
1209 * already loaded and the module being loaded is not a
1210 * softdep, bail out. Otherwise, just ignore and continue.
1211 *
1212 * We need to check here because of race conditions. We
1213 * checked first if module was already loaded but it may have
1214 * been loaded between the check and the moment we try to
1215 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001216 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001217 if (err == -EEXIST && m == mod &&
1218 (flags & KMOD_PROBE_STOP_ON_ALREADY_LOADED)) {
1219 err = KMOD_PROBE_STOP_ON_ALREADY_LOADED;
1220 break;
1221 }
1222
Lucas De Marchi79d6c7d2012-01-30 16:30:12 -02001223 if (err < 0 && err != -EEXIST)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001224 break;
1225 }
1226
1227 kmod_module_unref_list(list);
1228 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001229}
1230
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001231/**
1232 * kmod_module_get_options:
1233 * @mod: kmod module
1234 *
1235 * Get options of this kmod module. Options come from the configuration file
1236 * and are cached in @mod. The first call to this function will search for
1237 * this module in configuration and subsequent calls return the cached string.
1238 *
1239 * Returns: a string with all the options separated by spaces. This string is
1240 * owned by @mod, do not free it.
1241 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001242KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1243{
1244 if (mod == NULL)
1245 return NULL;
1246
1247 if (!mod->init.options) {
1248 /* lazy init */
1249 struct kmod_module *m = (struct kmod_module *)mod;
1250 const struct kmod_list *l, *ctx_options;
1251 char *opts = NULL;
1252 size_t optslen = 0;
1253
1254 ctx_options = kmod_get_options(mod->ctx);
1255
1256 kmod_list_foreach(l, ctx_options) {
1257 const char *modname = kmod_option_get_modname(l);
1258 const char *str;
1259 size_t len;
1260 void *tmp;
1261
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001262 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1263 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1264 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001265 continue;
1266
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001267 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 -02001268 str = kmod_option_get_options(l);
1269 len = strlen(str);
1270 if (len < 1)
1271 continue;
1272
1273 tmp = realloc(opts, optslen + len + 2);
1274 if (tmp == NULL) {
1275 free(opts);
1276 goto failed;
1277 }
1278
1279 opts = tmp;
1280
1281 if (optslen > 0) {
1282 opts[optslen] = ' ';
1283 optslen++;
1284 }
1285
1286 memcpy(opts + optslen, str, len);
1287 optslen += len;
1288 opts[optslen] = '\0';
1289 }
1290
1291 m->init.options = true;
1292 m->options = opts;
1293 }
1294
1295 return mod->options;
1296
1297failed:
1298 ERR(mod->ctx, "out of memory\n");
1299 return NULL;
1300}
1301
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001302/**
1303 * kmod_module_get_install_commands:
1304 * @mod: kmod module
1305 *
1306 * Get install commands for this kmod module. Install commands come from the
1307 * configuration file and are cached in @mod. The first call to this function
1308 * will search for this module in configuration and subsequent calls return
1309 * the cached string. The install commands are returned as they were in the
1310 * configuration, concatenated by ';'. No other processing is made in this
1311 * string.
1312 *
1313 * Returns: a string with all install commands separated by semicolons. This
1314 * string is owned by @mod, do not free it.
1315 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001316KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1317{
1318 if (mod == NULL)
1319 return NULL;
1320
1321 if (!mod->init.install_commands) {
1322 /* lazy init */
1323 struct kmod_module *m = (struct kmod_module *)mod;
1324 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001325
1326 ctx_install_commands = kmod_get_install_commands(mod->ctx);
1327
1328 kmod_list_foreach(l, ctx_install_commands) {
1329 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001330
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001331 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001332 continue;
1333
Lucas De Marchi60f67602011-12-16 03:33:26 -02001334 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001335
Lucas De Marchi60f67602011-12-16 03:33:26 -02001336 /*
1337 * find only the first command, as modprobe from
1338 * module-init-tools does
1339 */
1340 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001341 }
1342
1343 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001344 }
1345
1346 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001347}
1348
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001349void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1350{
1351 mod->init.install_commands = true;
1352 mod->install_commands = cmd;
1353}
1354
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001355static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1356{
1357 struct kmod_list *ret = NULL;
1358 unsigned i;
1359
1360 for (i = 0; i < count; i++) {
1361 const char *depname = array[i];
1362 struct kmod_list *lst = NULL;
1363 int err;
1364
1365 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1366 if (err < 0) {
1367 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1368 continue;
1369 } else if (lst != NULL)
1370 ret = kmod_list_append_list(ret, lst);
1371 }
1372 return ret;
1373}
1374
1375/**
1376 * kmod_module_get_softdeps:
1377 * @mod: kmod module
1378 * @pre: where to save the list of preceding soft dependencies.
1379 * @post: where to save the list of post soft dependencies.
1380 *
1381 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001382 * from configuration file and are not cached in @mod because it may include
1383 * dependency cycles that would make we leak kmod_module. Any call
1384 * to this function will search for this module in configuration, allocate a
1385 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001386 *
1387 * Both @pre and @post are newly created list of kmod_module and
1388 * should be unreferenced with kmod_module_unref_list().
1389 *
1390 * Returns: 0 on success or < 0 otherwise.
1391 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001392KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1393 struct kmod_list **pre,
1394 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001395{
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001396 const struct kmod_list *l, *ctx_softdeps;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001397
1398 if (mod == NULL || pre == NULL || post == NULL)
1399 return -ENOENT;
1400
1401 assert(*pre == NULL);
1402 assert(*post == NULL);
1403
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001404 ctx_softdeps = kmod_get_softdeps(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001405
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001406 kmod_list_foreach(l, ctx_softdeps) {
1407 const char *modname = kmod_softdep_get_name(l);
1408 const char * const *array;
1409 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001410
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001411 if (fnmatch(modname, mod->name, 0) != 0)
1412 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001413
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001414 array = kmod_softdep_get_pre(l, &count);
1415 *pre = lookup_softdep(mod->ctx, array, count);
1416 array = kmod_softdep_get_post(l, &count);
1417 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001418
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001419 /*
1420 * find only the first command, as modprobe from
1421 * module-init-tools does
1422 */
1423 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001424 }
1425
1426 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001427}
1428
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001429/**
1430 * kmod_module_get_remove_commands:
1431 * @mod: kmod module
1432 *
1433 * Get remove commands for this kmod module. Remove commands come from the
1434 * configuration file and are cached in @mod. The first call to this function
1435 * will search for this module in configuration and subsequent calls return
1436 * the cached string. The remove commands are returned as they were in the
1437 * configuration, concatenated by ';'. No other processing is made in this
1438 * string.
1439 *
1440 * Returns: a string with all remove commands separated by semicolons. This
1441 * string is owned by @mod, do not free it.
1442 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001443KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1444{
1445 if (mod == NULL)
1446 return NULL;
1447
1448 if (!mod->init.remove_commands) {
1449 /* lazy init */
1450 struct kmod_module *m = (struct kmod_module *)mod;
1451 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001452
1453 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1454
1455 kmod_list_foreach(l, ctx_remove_commands) {
1456 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001457
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001458 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001459 continue;
1460
Lucas De Marchi60f67602011-12-16 03:33:26 -02001461 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001462
Lucas De Marchi60f67602011-12-16 03:33:26 -02001463 /*
1464 * find only the first command, as modprobe from
1465 * module-init-tools does
1466 */
1467 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001468 }
1469
1470 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001471 }
1472
1473 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001474}
1475
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001476void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1477{
1478 mod->init.remove_commands = true;
1479 mod->remove_commands = cmd;
1480}
1481
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001482/**
1483 * SECTION:libkmod-loaded
1484 * @short_description: currently loaded modules
1485 *
1486 * Information about currently loaded modules, as reported by Linux kernel.
1487 * These information are not cached by libkmod and are always read from /sys
1488 * and /proc/modules.
1489 */
1490
1491/**
1492 * kmod_module_new_from_loaded:
1493 * @ctx: kmod library context
1494 * @list: where to save the list of loaded modules
1495 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001496 * Create a new list of kmod modules with all modules currently loaded in
1497 * kernel. It uses /proc/modules to get the names of loaded modules and to
1498 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1499 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001500 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001501 * The initial refcount is 1, and needs to be decremented to release the
1502 * resources of the kmod_module. The returned @list must be released by
1503 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1504 * kmod_modules created, they are all released upon @ctx destruction too. Do
1505 * not unref @ctx before all the desired operations with the returned list are
1506 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001507 *
1508 * Returns: 0 on success or < 0 on error.
1509 */
1510KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1511 struct kmod_list **list)
1512{
1513 struct kmod_list *l = NULL;
1514 FILE *fp;
1515 char line[4096];
1516
1517 if (ctx == NULL || list == NULL)
1518 return -ENOENT;
1519
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001520 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001521 if (fp == NULL) {
1522 int err = -errno;
1523 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1524 return err;
1525 }
1526
1527 while (fgets(line, sizeof(line), fp)) {
1528 struct kmod_module *m;
1529 struct kmod_list *node;
1530 int err;
1531 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1532
1533 err = kmod_module_new_from_name(ctx, name, &m);
1534 if (err < 0) {
1535 ERR(ctx, "could not get module from name '%s': %s\n",
1536 name, strerror(-err));
1537 continue;
1538 }
1539
1540 node = kmod_list_append(l, m);
1541 if (node)
1542 l = node;
1543 else {
1544 ERR(ctx, "out of memory\n");
1545 kmod_module_unref(m);
1546 }
1547 }
1548
1549 fclose(fp);
1550 *list = l;
1551
1552 return 0;
1553}
1554
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001555/**
1556 * kmod_module_initstate_str:
1557 * @state: the state as returned by kmod_module_get_initstate()
1558 *
1559 * Translate a initstate to a string.
1560 *
1561 * Returns: the string associated to the @state. This string is statically
1562 * allocated, do not free it.
1563 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001564KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1565{
1566 switch (state) {
1567 case KMOD_MODULE_BUILTIN:
1568 return "builtin";
1569 case KMOD_MODULE_LIVE:
1570 return "live";
1571 case KMOD_MODULE_COMING:
1572 return "coming";
1573 case KMOD_MODULE_GOING:
1574 return "going";
1575 default:
1576 return NULL;
1577 }
1578}
1579
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001580/**
1581 * kmod_module_get_initstate:
1582 * @mod: kmod module
1583 *
1584 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1585 * /sys filesystem.
1586 *
1587 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1588 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001589KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1590{
1591 char path[PATH_MAX], buf[32];
1592 int fd, err, pathlen;
1593
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001594 if (mod == NULL)
1595 return -ENOENT;
1596
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001597 pathlen = snprintf(path, sizeof(path),
1598 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001599 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001600 if (fd < 0) {
1601 err = -errno;
1602
Lucas De Marchiddbda022011-12-27 11:40:10 -02001603 DBG(mod->ctx, "could not open '%s': %s\n",
1604 path, strerror(-err));
1605
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001606 if (pathlen > (int)sizeof("/initstate") - 1) {
1607 struct stat st;
1608 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1609 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1610 return KMOD_MODULE_BUILTIN;
1611 }
1612
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001613 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001614 path, strerror(-err));
1615 return err;
1616 }
1617
1618 err = read_str_safe(fd, buf, sizeof(buf));
1619 close(fd);
1620 if (err < 0) {
1621 ERR(mod->ctx, "could not read from '%s': %s\n",
1622 path, strerror(-err));
1623 return err;
1624 }
1625
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001626 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001627 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001628 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001629 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001630 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001631 return KMOD_MODULE_GOING;
1632
1633 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1634 return -EINVAL;
1635}
1636
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001637/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001638 * kmod_module_get_size:
1639 * @mod: kmod module
1640 *
1641 * Get the size of this kmod module as returned by Linux kernel. It reads the
1642 * file /proc/modules to search for this module and get its size.
1643 *
1644 * Returns: the size of this kmod module.
1645 */
1646KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1647{
1648 // FIXME TODO: this should be available from /sys/module/foo
1649 FILE *fp;
1650 char line[4096];
1651 int lineno = 0;
1652 long size = -ENOENT;
1653
1654 if (mod == NULL)
1655 return -ENOENT;
1656
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001657 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001658 if (fp == NULL) {
1659 int err = -errno;
1660 ERR(mod->ctx,
1661 "could not open /proc/modules: %s\n", strerror(errno));
1662 return err;
1663 }
1664
1665 while (fgets(line, sizeof(line), fp)) {
1666 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1667 long value;
1668
1669 lineno++;
1670 if (tok == NULL || !streq(tok, mod->name))
1671 continue;
1672
1673 tok = strtok_r(NULL, " \t", &saveptr);
1674 if (tok == NULL) {
1675 ERR(mod->ctx,
1676 "invalid line format at /proc/modules:%d\n", lineno);
1677 break;
1678 }
1679
1680 value = strtol(tok, &endptr, 10);
1681 if (endptr == tok || *endptr != '\0') {
1682 ERR(mod->ctx,
1683 "invalid line format at /proc/modules:%d\n", lineno);
1684 break;
1685 }
1686
1687 size = value;
1688 break;
1689 }
1690 fclose(fp);
1691 return size;
1692}
1693
1694/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001695 * kmod_module_get_refcnt:
1696 * @mod: kmod module
1697 *
1698 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1699 * /sys filesystem.
1700 *
1701 * Returns: 0 on success or < 0 on failure.
1702 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001703KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1704{
1705 char path[PATH_MAX];
1706 long refcnt;
1707 int fd, err;
1708
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001709 if (mod == NULL)
1710 return -ENOENT;
1711
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001712 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001713 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001714 if (fd < 0) {
1715 err = -errno;
1716 ERR(mod->ctx, "could not open '%s': %s\n",
1717 path, strerror(errno));
1718 return err;
1719 }
1720
1721 err = read_str_long(fd, &refcnt, 10);
1722 close(fd);
1723 if (err < 0) {
1724 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1725 path, strerror(-err));
1726 return err;
1727 }
1728
1729 return (int)refcnt;
1730}
1731
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001732/**
1733 * kmod_module_get_holders:
1734 * @mod: kmod module
1735 *
1736 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1737 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1738 *
1739 * Returns: a new list of kmod modules on success or NULL on failure.
1740 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001741KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1742{
1743 char dname[PATH_MAX];
1744 struct kmod_list *list = NULL;
1745 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001746
1747 if (mod == NULL)
1748 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001749
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001750 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1751
1752 d = opendir(dname);
1753 if (d == NULL) {
1754 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001755 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001756 return NULL;
1757 }
1758
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001759 for (;;) {
1760 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001761 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001762 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001763 int err;
1764
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001765 err = readdir_r(d, &de, &entp);
1766 if (err != 0) {
1767 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1768 mod->name, strerror(-err));
1769 goto fail;
1770 }
1771
1772 if (entp == NULL)
1773 break;
1774
1775 if (de.d_name[0] == '.') {
1776 if (de.d_name[1] == '\0' ||
1777 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001778 continue;
1779 }
1780
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001781 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001782 if (err < 0) {
1783 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001784 de.d_name, strerror(-err));
1785 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001786 }
1787
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001788 l = kmod_list_append(list, holder);
1789 if (l != NULL) {
1790 list = l;
1791 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001792 ERR(mod->ctx, "out of memory\n");
1793 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001794 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001795 }
1796 }
1797
1798 closedir(d);
1799 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001800
1801fail:
1802 closedir(d);
1803 kmod_module_unref_list(list);
1804 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001805}
1806
1807struct kmod_module_section {
1808 unsigned long address;
1809 char name[];
1810};
1811
1812static void kmod_module_section_free(struct kmod_module_section *section)
1813{
1814 free(section);
1815}
1816
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001817/**
1818 * kmod_module_get_sections:
1819 * @mod: kmod module
1820 *
1821 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1822 * structure contained in this list is internal to libkmod and their fields
1823 * can be obtained by calling kmod_module_section_get_name() and
1824 * kmod_module_section_get_address().
1825 *
1826 * After use, free the @list by calling kmod_module_section_free_list().
1827 *
1828 * Returns: a new list of kmod module sections on success or NULL on failure.
1829 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001830KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1831{
1832 char dname[PATH_MAX];
1833 struct kmod_list *list = NULL;
1834 DIR *d;
1835 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001836
1837 if (mod == NULL)
1838 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001839
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001840 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1841
1842 d = opendir(dname);
1843 if (d == NULL) {
1844 ERR(mod->ctx, "could not open '%s': %s\n",
1845 dname, strerror(errno));
1846 return NULL;
1847 }
1848
1849 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001850
1851 for (;;) {
1852 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001853 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001854 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001855 unsigned long address;
1856 size_t namesz;
1857 int fd, err;
1858
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001859 err = readdir_r(d, &de, &entp);
1860 if (err != 0) {
1861 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1862 mod->name, strerror(-err));
1863 goto fail;
1864 }
1865
1866 if (de.d_name[0] == '.') {
1867 if (de.d_name[1] == '\0' ||
1868 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001869 continue;
1870 }
1871
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001872 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001873 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001874 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1875 dname, de.d_name);
1876 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001877 }
1878
1879 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001880 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001881 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001882 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1883 dname, de.d_name);
1884 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001885 }
1886
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001887 namesz = strlen(de.d_name) + 1;
1888 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001889
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001890 if (section == NULL) {
1891 ERR(mod->ctx, "out of memory\n");
1892 goto fail;
1893 }
1894
1895 section->address = address;
1896 memcpy(section->name, de.d_name, namesz);
1897
1898 l = kmod_list_append(list, section);
1899 if (l != NULL) {
1900 list = l;
1901 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001902 ERR(mod->ctx, "out of memory\n");
1903 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001904 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001905 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001906 }
1907
1908 closedir(d);
1909 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001910
1911fail:
1912 closedir(d);
1913 kmod_module_unref_list(list);
1914 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001915}
1916
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001917/**
1918 * kmod_module_section_get_module_name:
1919 * @entry: a list entry representing a kmod module section
1920 *
1921 * Get the name of a kmod module section.
1922 *
1923 * After use, free the @list by calling kmod_module_section_free_list().
1924 *
1925 * Returns: the name of this kmod module section on success or NULL on
1926 * failure. The string is owned by the section, do not free it.
1927 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001928KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1929{
1930 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001931
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001932 if (entry == NULL)
1933 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001934
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001935 section = entry->data;
1936 return section->name;
1937}
1938
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001939/**
1940 * kmod_module_section_get_address:
1941 * @entry: a list entry representing a kmod module section
1942 *
1943 * Get the address of a kmod module section.
1944 *
1945 * After use, free the @list by calling kmod_module_section_free_list().
1946 *
1947 * Returns: the address of this kmod module section on success or ULONG_MAX
1948 * on failure.
1949 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001950KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1951{
1952 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001953
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001954 if (entry == NULL)
1955 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001956
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001957 section = entry->data;
1958 return section->address;
1959}
1960
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001961/**
1962 * kmod_module_section_free_list:
1963 * @list: kmod module section list
1964 *
1965 * Release the resources taken by @list
1966 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001967KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1968{
1969 while (list) {
1970 kmod_module_section_free(list->data);
1971 list = kmod_list_remove(list);
1972 }
1973}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001974
1975struct kmod_module_info {
1976 char *key;
1977 char value[];
1978};
1979
1980static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
1981{
1982 struct kmod_module_info *info;
1983
1984 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
1985 if (info == NULL)
1986 return NULL;
1987
1988 info->key = (char *)info + sizeof(struct kmod_module_info)
1989 + valuelen + 1;
1990 memcpy(info->key, key, keylen);
1991 info->key[keylen] = '\0';
1992 memcpy(info->value, value, valuelen);
1993 info->value[valuelen] = '\0';
1994 return info;
1995}
1996
1997static void kmod_module_info_free(struct kmod_module_info *info)
1998{
1999 free(info);
2000}
2001
2002/**
2003 * kmod_module_get_info:
2004 * @mod: kmod module
2005 * @list: where to return list of module information. Use
2006 * kmod_module_info_get_key() and
2007 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002008 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002009 *
2010 * Get a list of entries in ELF section ".modinfo", these contain
2011 * alias, license, depends, vermagic and other keys with respective
2012 * values.
2013 *
2014 * After use, free the @list by calling kmod_module_info_free_list().
2015 *
2016 * Returns: 0 on success or < 0 otherwise.
2017 */
2018KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2019{
2020 struct kmod_file *file;
2021 struct kmod_elf *elf;
2022 const char *path;
2023 const void *mem;
2024 char **strings;
2025 size_t size;
2026 int i, count, ret = 0;
2027
2028 if (mod == NULL || list == NULL)
2029 return -ENOENT;
2030
2031 assert(*list == NULL);
2032
2033 path = kmod_module_get_path(mod);
2034 if (path == NULL)
2035 return -ENOENT;
2036
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002037 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002038 if (file == NULL)
2039 return -errno;
2040
2041 size = kmod_file_get_size(file);
2042 mem = kmod_file_get_contents(file);
2043
2044 elf = kmod_elf_new(mem, size);
2045 if (elf == NULL) {
2046 ret = -errno;
2047 goto elf_open_error;
2048 }
2049
2050 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
2051 if (count < 0) {
2052 ret = count;
2053 goto get_strings_error;
2054 }
2055
2056 for (i = 0; i < count; i++) {
2057 struct kmod_module_info *info;
2058 struct kmod_list *n;
2059 const char *key, *value;
2060 size_t keylen, valuelen;
2061
2062 key = strings[i];
2063 value = strchr(key, '=');
2064 if (value == NULL) {
2065 keylen = strlen(key);
2066 valuelen = 0;
2067 } else {
2068 keylen = value - key;
2069 value++;
2070 valuelen = strlen(value);
2071 }
2072
2073 info = kmod_module_info_new(key, keylen, value, valuelen);
2074 if (info == NULL) {
2075 ret = -errno;
2076 kmod_module_info_free_list(*list);
2077 *list = NULL;
2078 goto list_error;
2079 }
2080
2081 n = kmod_list_append(*list, info);
2082 if (n != NULL)
2083 *list = n;
2084 else {
2085 kmod_module_info_free(info);
2086 kmod_module_info_free_list(*list);
2087 *list = NULL;
2088 ret = -ENOMEM;
2089 goto list_error;
2090 }
2091 }
2092 ret = count;
2093
2094list_error:
2095 free(strings);
2096get_strings_error:
2097 kmod_elf_unref(elf);
2098elf_open_error:
2099 kmod_file_unref(file);
2100
2101 return ret;
2102}
2103
2104/**
2105 * kmod_module_info_get_key:
2106 * @entry: a list entry representing a kmod module info
2107 *
2108 * Get the key of a kmod module info.
2109 *
2110 * Returns: the key of this kmod module info on success or NULL on
2111 * failure. The string is owned by the info, do not free it.
2112 */
2113KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2114{
2115 struct kmod_module_info *info;
2116
2117 if (entry == NULL)
2118 return NULL;
2119
2120 info = entry->data;
2121 return info->key;
2122}
2123
2124/**
2125 * kmod_module_info_get_value:
2126 * @entry: a list entry representing a kmod module info
2127 *
2128 * Get the value of a kmod module info.
2129 *
2130 * Returns: the value of this kmod module info on success or NULL on
2131 * failure. The string is owned by the info, do not free it.
2132 */
2133KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2134{
2135 struct kmod_module_info *info;
2136
2137 if (entry == NULL)
2138 return NULL;
2139
2140 info = entry->data;
2141 return info->value;
2142}
2143
2144/**
2145 * kmod_module_info_free_list:
2146 * @list: kmod module info list
2147 *
2148 * Release the resources taken by @list
2149 */
2150KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2151{
2152 while (list) {
2153 kmod_module_info_free(list->data);
2154 list = kmod_list_remove(list);
2155 }
2156}
2157
2158struct kmod_module_version {
2159 uint64_t crc;
2160 char symbol[];
2161};
2162
2163static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2164{
2165 struct kmod_module_version *mv;
2166 size_t symbollen = strlen(symbol) + 1;
2167
2168 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2169 if (mv == NULL)
2170 return NULL;
2171
2172 mv->crc = crc;
2173 memcpy(mv->symbol, symbol, symbollen);
2174 return mv;
2175}
2176
2177static void kmod_module_version_free(struct kmod_module_version *version)
2178{
2179 free(version);
2180}
2181
2182/**
2183 * kmod_module_get_versions:
2184 * @mod: kmod module
2185 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002186 * kmod_module_version_get_symbol() and
2187 * kmod_module_version_get_crc(). Release this list with
2188 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002189 *
2190 * Get a list of entries in ELF section "__versions".
2191 *
2192 * After use, free the @list by calling kmod_module_versions_free_list().
2193 *
2194 * Returns: 0 on success or < 0 otherwise.
2195 */
2196KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2197{
2198 struct kmod_file *file;
2199 struct kmod_elf *elf;
2200 const char *path;
2201 const void *mem;
2202 struct kmod_modversion *versions;
2203 size_t size;
2204 int i, count, ret = 0;
2205
2206 if (mod == NULL || list == NULL)
2207 return -ENOENT;
2208
2209 assert(*list == NULL);
2210
2211 path = kmod_module_get_path(mod);
2212 if (path == NULL)
2213 return -ENOENT;
2214
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002215 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002216 if (file == NULL)
2217 return -errno;
2218
2219 size = kmod_file_get_size(file);
2220 mem = kmod_file_get_contents(file);
2221
2222 elf = kmod_elf_new(mem, size);
2223 if (elf == NULL) {
2224 ret = -errno;
2225 goto elf_open_error;
2226 }
2227
2228 count = kmod_elf_get_modversions(elf, &versions);
2229 if (count < 0) {
2230 ret = count;
2231 goto get_strings_error;
2232 }
2233
2234 for (i = 0; i < count; i++) {
2235 struct kmod_module_version *mv;
2236 struct kmod_list *n;
2237
2238 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2239 if (mv == NULL) {
2240 ret = -errno;
2241 kmod_module_versions_free_list(*list);
2242 *list = NULL;
2243 goto list_error;
2244 }
2245
2246 n = kmod_list_append(*list, mv);
2247 if (n != NULL)
2248 *list = n;
2249 else {
2250 kmod_module_version_free(mv);
2251 kmod_module_versions_free_list(*list);
2252 *list = NULL;
2253 ret = -ENOMEM;
2254 goto list_error;
2255 }
2256 }
2257 ret = count;
2258
2259list_error:
2260 free(versions);
2261get_strings_error:
2262 kmod_elf_unref(elf);
2263elf_open_error:
2264 kmod_file_unref(file);
2265
2266 return ret;
2267}
2268
2269/**
2270 * kmod_module_versions_get_symbol:
2271 * @entry: a list entry representing a kmod module versions
2272 *
2273 * Get the symbol of a kmod module versions.
2274 *
2275 * Returns: the symbol of this kmod module versions on success or NULL
2276 * on failure. The string is owned by the versions, do not free it.
2277 */
2278KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2279{
2280 struct kmod_module_version *version;
2281
2282 if (entry == NULL)
2283 return NULL;
2284
2285 version = entry->data;
2286 return version->symbol;
2287}
2288
2289/**
2290 * kmod_module_version_get_crc:
2291 * @entry: a list entry representing a kmod module version
2292 *
2293 * Get the crc of a kmod module version.
2294 *
2295 * Returns: the crc of this kmod module version on success or NULL on
2296 * failure. The string is owned by the version, do not free it.
2297 */
2298KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2299{
2300 struct kmod_module_version *version;
2301
2302 if (entry == NULL)
2303 return 0;
2304
2305 version = entry->data;
2306 return version->crc;
2307}
2308
2309/**
2310 * kmod_module_versions_free_list:
2311 * @list: kmod module versions list
2312 *
2313 * Release the resources taken by @list
2314 */
2315KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2316{
2317 while (list) {
2318 kmod_module_version_free(list->data);
2319 list = kmod_list_remove(list);
2320 }
2321}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002322
2323struct kmod_module_symbol {
2324 uint64_t crc;
2325 char symbol[];
2326};
2327
2328static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2329{
2330 struct kmod_module_symbol *mv;
2331 size_t symbollen = strlen(symbol) + 1;
2332
2333 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2334 if (mv == NULL)
2335 return NULL;
2336
2337 mv->crc = crc;
2338 memcpy(mv->symbol, symbol, symbollen);
2339 return mv;
2340}
2341
2342static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2343{
2344 free(symbol);
2345}
2346
2347/**
2348 * kmod_module_get_symbols:
2349 * @mod: kmod module
2350 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002351 * kmod_module_symbol_get_symbol() and
2352 * kmod_module_symbol_get_crc(). Release this list with
2353 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002354 *
2355 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2356 *
2357 * After use, free the @list by calling kmod_module_symbols_free_list().
2358 *
2359 * Returns: 0 on success or < 0 otherwise.
2360 */
2361KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2362{
2363 struct kmod_file *file;
2364 struct kmod_elf *elf;
2365 const char *path;
2366 const void *mem;
2367 struct kmod_modversion *symbols;
2368 size_t size;
2369 int i, count, ret = 0;
2370
2371 if (mod == NULL || list == NULL)
2372 return -ENOENT;
2373
2374 assert(*list == NULL);
2375
2376 path = kmod_module_get_path(mod);
2377 if (path == NULL)
2378 return -ENOENT;
2379
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002380 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002381 if (file == NULL)
2382 return -errno;
2383
2384 size = kmod_file_get_size(file);
2385 mem = kmod_file_get_contents(file);
2386
2387 elf = kmod_elf_new(mem, size);
2388 if (elf == NULL) {
2389 ret = -errno;
2390 goto elf_open_error;
2391 }
2392
2393 count = kmod_elf_get_symbols(elf, &symbols);
2394 if (count < 0) {
2395 ret = count;
2396 goto get_strings_error;
2397 }
2398
2399 for (i = 0; i < count; i++) {
2400 struct kmod_module_symbol *mv;
2401 struct kmod_list *n;
2402
2403 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2404 if (mv == NULL) {
2405 ret = -errno;
2406 kmod_module_symbols_free_list(*list);
2407 *list = NULL;
2408 goto list_error;
2409 }
2410
2411 n = kmod_list_append(*list, mv);
2412 if (n != NULL)
2413 *list = n;
2414 else {
2415 kmod_module_symbol_free(mv);
2416 kmod_module_symbols_free_list(*list);
2417 *list = NULL;
2418 ret = -ENOMEM;
2419 goto list_error;
2420 }
2421 }
2422 ret = count;
2423
2424list_error:
2425 free(symbols);
2426get_strings_error:
2427 kmod_elf_unref(elf);
2428elf_open_error:
2429 kmod_file_unref(file);
2430
2431 return ret;
2432}
2433
2434/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002435 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002436 * @entry: a list entry representing a kmod module symbols
2437 *
2438 * Get the symbol of a kmod module symbols.
2439 *
2440 * Returns: the symbol of this kmod module symbols on success or NULL
2441 * on failure. The string is owned by the symbols, do not free it.
2442 */
2443KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2444{
2445 struct kmod_module_symbol *symbol;
2446
2447 if (entry == NULL)
2448 return NULL;
2449
2450 symbol = entry->data;
2451 return symbol->symbol;
2452}
2453
2454/**
2455 * kmod_module_symbol_get_crc:
2456 * @entry: a list entry representing a kmod module symbol
2457 *
2458 * Get the crc of a kmod module symbol.
2459 *
2460 * Returns: the crc of this kmod module symbol on success or NULL on
2461 * failure. The string is owned by the symbol, do not free it.
2462 */
2463KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2464{
2465 struct kmod_module_symbol *symbol;
2466
2467 if (entry == NULL)
2468 return 0;
2469
2470 symbol = entry->data;
2471 return symbol->crc;
2472}
2473
2474/**
2475 * kmod_module_symbols_free_list:
2476 * @list: kmod module symbols list
2477 *
2478 * Release the resources taken by @list
2479 */
2480KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2481{
2482 while (list) {
2483 kmod_module_symbol_free(list->data);
2484 list = kmod_list_remove(list);
2485 }
2486}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002487
2488struct kmod_module_dependency_symbol {
2489 uint64_t crc;
2490 uint8_t bind;
2491 char symbol[];
2492};
2493
2494static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2495{
2496 struct kmod_module_dependency_symbol *mv;
2497 size_t symbollen = strlen(symbol) + 1;
2498
2499 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2500 if (mv == NULL)
2501 return NULL;
2502
2503 mv->crc = crc;
2504 mv->bind = bind;
2505 memcpy(mv->symbol, symbol, symbollen);
2506 return mv;
2507}
2508
2509static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2510{
2511 free(dependency_symbol);
2512}
2513
2514/**
2515 * kmod_module_get_dependency_symbols:
2516 * @mod: kmod module
2517 * @list: where to return list of module dependency_symbols. Use
2518 * kmod_module_dependency_symbol_get_symbol() and
2519 * kmod_module_dependency_symbol_get_crc(). Release this list with
2520 * kmod_module_dependency_symbols_free_list()
2521 *
2522 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2523 *
2524 * After use, free the @list by calling
2525 * kmod_module_dependency_symbols_free_list().
2526 *
2527 * Returns: 0 on success or < 0 otherwise.
2528 */
2529KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2530{
2531 struct kmod_file *file;
2532 struct kmod_elf *elf;
2533 const char *path;
2534 const void *mem;
2535 struct kmod_modversion *symbols;
2536 size_t size;
2537 int i, count, ret = 0;
2538
2539 if (mod == NULL || list == NULL)
2540 return -ENOENT;
2541
2542 assert(*list == NULL);
2543
2544 path = kmod_module_get_path(mod);
2545 if (path == NULL)
2546 return -ENOENT;
2547
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002548 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002549 if (file == NULL)
2550 return -errno;
2551
2552 size = kmod_file_get_size(file);
2553 mem = kmod_file_get_contents(file);
2554
2555 elf = kmod_elf_new(mem, size);
2556 if (elf == NULL) {
2557 ret = -errno;
2558 goto elf_open_error;
2559 }
2560
2561 count = kmod_elf_get_dependency_symbols(elf, &symbols);
2562 if (count < 0) {
2563 ret = count;
2564 goto get_strings_error;
2565 }
2566
2567 for (i = 0; i < count; i++) {
2568 struct kmod_module_dependency_symbol *mv;
2569 struct kmod_list *n;
2570
2571 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2572 symbols[i].bind,
2573 symbols[i].symbol);
2574 if (mv == NULL) {
2575 ret = -errno;
2576 kmod_module_dependency_symbols_free_list(*list);
2577 *list = NULL;
2578 goto list_error;
2579 }
2580
2581 n = kmod_list_append(*list, mv);
2582 if (n != NULL)
2583 *list = n;
2584 else {
2585 kmod_module_dependency_symbol_free(mv);
2586 kmod_module_dependency_symbols_free_list(*list);
2587 *list = NULL;
2588 ret = -ENOMEM;
2589 goto list_error;
2590 }
2591 }
2592 ret = count;
2593
2594list_error:
2595 free(symbols);
2596get_strings_error:
2597 kmod_elf_unref(elf);
2598elf_open_error:
2599 kmod_file_unref(file);
2600
2601 return ret;
2602}
2603
2604/**
2605 * kmod_module_dependency_symbol_get_symbol:
2606 * @entry: a list entry representing a kmod module dependency_symbols
2607 *
2608 * Get the dependency symbol of a kmod module
2609 *
2610 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2611 * on failure. The string is owned by the dependency_symbols, do not free it.
2612 */
2613KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2614{
2615 struct kmod_module_dependency_symbol *dependency_symbol;
2616
2617 if (entry == NULL)
2618 return NULL;
2619
2620 dependency_symbol = entry->data;
2621 return dependency_symbol->symbol;
2622}
2623
2624/**
2625 * kmod_module_dependency_symbol_get_crc:
2626 * @entry: a list entry representing a kmod module dependency_symbol
2627 *
2628 * Get the crc of a kmod module dependency_symbol.
2629 *
2630 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2631 * failure. The string is owned by the dependency_symbol, do not free it.
2632 */
2633KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2634{
2635 struct kmod_module_dependency_symbol *dependency_symbol;
2636
2637 if (entry == NULL)
2638 return 0;
2639
2640 dependency_symbol = entry->data;
2641 return dependency_symbol->crc;
2642}
2643
2644/**
2645 * kmod_module_dependency_symbol_get_bind:
2646 * @entry: a list entry representing a kmod module dependency_symbol
2647 *
2648 * Get the bind type of a kmod module dependency_symbol.
2649 *
2650 * Returns: the bind of this kmod module dependency_symbol on success
2651 * or < 0 on failure.
2652 */
2653KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2654{
2655 struct kmod_module_dependency_symbol *dependency_symbol;
2656
2657 if (entry == NULL)
2658 return 0;
2659
2660 dependency_symbol = entry->data;
2661 return dependency_symbol->bind;
2662}
2663
2664/**
2665 * kmod_module_dependency_symbols_free_list:
2666 * @list: kmod module dependency_symbols list
2667 *
2668 * Release the resources taken by @list
2669 */
2670KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2671{
2672 while (list) {
2673 kmod_module_dependency_symbol_free(list->data);
2674 list = kmod_list_remove(list);
2675 }
2676}