blob: b1d40b197a57b3e29a6eb8249ce04c20e88a339b [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Lucas De Marchi8f788d52011-11-25 01:22:56 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi8f788d52011-11-25 01:22:56 -020010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Lucas De Marchi7636e722011-12-01 17:56:03 -020021#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020022#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020031#include <limits.h>
32#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/mman.h>
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 */
Lucas De Marchi1eff9422012-10-18 01:36:33 -030063 struct kmod_file *file;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020064 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020065 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020066 struct {
67 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020068 bool options : 1;
69 bool install_commands : 1;
70 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020071 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020072
73 /*
74 * private field used by kmod_module_get_probe_list() to detect
75 * dependency loops
76 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020077 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020078
79 /*
80 * set by kmod_module_get_probe_list: indicates for probe_insert()
81 * whether the module's command and softdep should be ignored
82 */
83 bool ignorecmd : 1;
Lucas De Marchi38052742012-02-16 20:43:16 -020084
85 /*
86 * if module was created by searching the modules.builtin file, this
87 * is set. There's nothing much useful one can do with such a
88 * "module", except knowing it's builtin.
89 */
90 bool builtin : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020091};
92
Lucas De Marchic35347f2011-12-12 10:48:02 -020093static inline const char *path_join(const char *path, size_t prefixlen,
94 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020095{
96 size_t pathlen;
97
98 if (path[0] == '/')
99 return path;
100
101 pathlen = strlen(path);
102 if (prefixlen + pathlen + 1 >= PATH_MAX)
103 return NULL;
104
105 memcpy(buf + prefixlen, path, pathlen + 1);
106 return buf;
107}
108
Dave Reisneraf9572c2012-02-02 11:07:33 -0500109static inline bool module_is_inkernel(struct kmod_module *mod)
110{
111 int state = kmod_module_get_initstate(mod);
Lucas De Marchi38052742012-02-16 20:43:16 -0200112
Dave Reisneraf9572c2012-02-02 11:07:33 -0500113 if (state == KMOD_MODULE_LIVE ||
Dave Reisneraf9572c2012-02-02 11:07:33 -0500114 state == KMOD_MODULE_BUILTIN)
115 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200116
117 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500118}
119
Lucas De Marchi671d4892011-12-05 20:23:05 -0200120int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200121{
122 struct kmod_ctx *ctx = mod->ctx;
123 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200124 const char *dirname;
125 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200126 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200127 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200128 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200129
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200130 if (mod->init.dep)
131 return mod->n_dep;
132 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200133 mod->init.dep = true;
134
135 p = strchr(line, ':');
136 if (p == NULL)
137 return 0;
138
Lucas De Marchi671d4892011-12-05 20:23:05 -0200139 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200140 dirname = kmod_get_dirname(mod->ctx);
141 dirnamelen = strlen(dirname);
142 if (dirnamelen + 2 >= PATH_MAX)
143 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200144
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200145 memcpy(buf, dirname, dirnamelen);
146 buf[dirnamelen] = '/';
147 dirnamelen++;
148 buf[dirnamelen] = '\0';
149
150 if (mod->path == NULL) {
151 const char *str = path_join(line, dirnamelen, buf);
152 if (str == NULL)
153 return 0;
154 mod->path = strdup(str);
155 if (mod->path == NULL)
156 return 0;
157 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200158
Lucas De Marchi7636e722011-12-01 17:56:03 -0200159 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200160 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
161 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200162 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200163 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200164
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200165 path = path_join(p, dirnamelen, buf);
166 if (path == NULL) {
167 ERR(ctx, "could not join path '%s' and '%s'.\n",
168 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200169 goto fail;
170 }
171
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200172 err = kmod_module_new_from_path(ctx, path, &depmod);
173 if (err < 0) {
174 ERR(ctx, "ctx=%p path=%s error=%s\n",
175 ctx, path, strerror(-err));
176 goto fail;
177 }
178
179 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200180
Lucas De Marchib94a7372011-12-26 20:10:49 -0200181 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200182 n++;
183 }
184
185 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
186
187 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200188 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200189 return n;
190
191fail:
192 kmod_module_unref_list(list);
193 mod->init.dep = false;
194 return err;
195}
196
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200197void kmod_module_set_visited(struct kmod_module *mod, bool visited)
198{
199 mod->visited = visited;
200}
201
Lucas De Marchi38052742012-02-16 20:43:16 -0200202void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
203{
204 mod->builtin = builtin;
205}
206
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200207/*
208 * Memory layout with alias:
209 *
210 * struct kmod_module {
211 * hashkey -----.
212 * alias -----. |
213 * name ----. | |
214 * } | | |
215 * name <----------' | |
216 * alias <-----------' |
217 * name\alias <--------'
218 *
219 * Memory layout without alias:
220 *
221 * struct kmod_module {
222 * hashkey ---.
223 * alias -----|----> NULL
224 * name ----. |
225 * } | |
226 * name <----------'-'
227 *
228 * @key is "name\alias" or "name" (in which case alias == NULL)
229 */
230static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
231 const char *name, size_t namelen,
232 const char *alias, size_t aliaslen,
233 struct kmod_module **mod)
234{
235 struct kmod_module *m;
236 size_t keylen;
237
238 m = kmod_pool_get_module(ctx, key);
239 if (m != NULL) {
240 *mod = kmod_module_ref(m);
241 return 0;
242 }
243
244 if (alias == NULL)
245 keylen = namelen;
246 else
247 keylen = namelen + aliaslen + 1;
248
249 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
250 if (m == NULL) {
251 free(m);
252 return -ENOMEM;
253 }
254
255 memset(m, 0, sizeof(*m));
256
257 m->ctx = kmod_ref(ctx);
258 m->name = (char *)m + sizeof(*m);
259 memcpy(m->name, key, keylen + 1);
260 if (alias == NULL) {
261 m->hashkey = m->name;
262 m->alias = NULL;
263 } else {
264 m->name[namelen] = '\0';
265 m->alias = m->name + namelen + 1;
266 m->hashkey = m->name + keylen + 1;
267 memcpy(m->hashkey, key, keylen + 1);
268 }
269
270 m->refcount = 1;
271 kmod_pool_add_module(ctx, m, m->hashkey);
272 *mod = m;
273
274 return 0;
275}
276
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200277/**
278 * kmod_module_new_from_name:
279 * @ctx: kmod library context
280 * @name: name of the module
281 * @mod: where to save the created struct kmod_module
282 *
283 * Create a new struct kmod_module using the module name. @name can not be an
284 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600285 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200286 *
287 * This function is also used internally by many others that return a new
288 * struct kmod_module or a new list of modules.
289 *
290 * The initial refcount is 1, and needs to be decremented to release the
291 * resources of the kmod_module. Since libkmod keeps track of all
292 * kmod_modules created, they are all released upon @ctx destruction too. Do
293 * not unref @ctx before all the desired operations with the returned
294 * kmod_module are done.
295 *
296 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
297 * module name or if memory allocation failed.
298 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200299KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
300 const char *name,
301 struct kmod_module **mod)
302{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200303 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200304 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200305
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200306 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200307 return -ENOENT;
308
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200309 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200310
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200311 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200312}
313
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200314int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
315 const char *name, struct kmod_module **mod)
316{
317 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200318 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200319 size_t namelen = strlen(name);
320 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200321
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200322 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200323 return -ENAMETOOLONG;
324
325 memcpy(key, name, namelen);
326 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200327 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200328
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200329 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200330 if (err < 0)
331 return err;
332
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200333 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200334}
335
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200336/**
337 * kmod_module_new_from_path:
338 * @ctx: kmod library context
339 * @path: path where to find the given module
340 * @mod: where to save the created struct kmod_module
341 *
342 * Create a new struct kmod_module using the module path. @path must be an
343 * existent file with in the filesystem and must be accessible to libkmod.
344 *
345 * The initial refcount is 1, and needs to be decremented to release the
346 * resources of the kmod_module. Since libkmod keeps track of all
347 * kmod_modules created, they are all released upon @ctx destruction too. Do
348 * not unref @ctx before all the desired operations with the returned
349 * kmod_module are done.
350 *
351 * If @path is relative, it's treated as relative to the current working
352 * directory. Otherwise, give an absolute path.
353 *
354 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
355 * it's not a valid file for a kmod_module or if memory allocation failed.
356 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200357KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
358 const char *path,
359 struct kmod_module **mod)
360{
361 struct kmod_module *m;
362 int err;
363 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200364 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200365 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200366 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200367
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200368 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200369 return -ENOENT;
370
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200371 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200372 if (abspath == NULL) {
373 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200374 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200375 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200376
377 err = stat(abspath, &st);
378 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200379 err = -errno;
380 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200381 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200382 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200383 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200384
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200385 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200386 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200387 free(abspath);
388 return -ENOENT;
389 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200390
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200391 m = kmod_pool_get_module(ctx, name);
392 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200393 if (m->path == NULL)
394 m->path = abspath;
395 else if (streq(m->path, abspath))
396 free(abspath);
397 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200398 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 -0200399 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200400 free(abspath);
401 return -EEXIST;
402 }
403
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200404 *mod = kmod_module_ref(m);
405 return 0;
406 }
407
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200408 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
409 if (err < 0)
410 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200411
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200412 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200413 *mod = m;
414
415 return 0;
416}
417
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200418/**
419 * kmod_module_unref:
420 * @mod: kmod module
421 *
422 * Drop a reference of the kmod module. If the refcount reaches zero, its
423 * resources are released.
424 *
425 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
426 * returns the passed @mod with its refcount decremented.
427 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200428KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
429{
430 if (mod == NULL)
431 return NULL;
432
433 if (--mod->refcount > 0)
434 return mod;
435
436 DBG(mod->ctx, "kmod_module %p released\n", mod);
437
Lucas De Marchi4084c172011-12-15 13:43:22 -0200438 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200439 kmod_module_unref_list(mod->dep);
Lucas De Marchi1eff9422012-10-18 01:36:33 -0300440
441 if (mod->file)
442 kmod_file_unref(mod->file);
443
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200444 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200445 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200446 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200447 free(mod);
448 return NULL;
449}
450
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200451/**
452 * kmod_module_ref:
453 * @mod: kmod module
454 *
455 * Take a reference of the kmod module, incrementing its refcount.
456 *
457 * Returns: the passed @module with its refcount incremented.
458 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200459KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
460{
461 if (mod == NULL)
462 return NULL;
463
464 mod->refcount++;
465
466 return mod;
467}
468
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200469#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
470 do { \
471 if ((_err) < 0) \
472 goto _label_err; \
473 if (*(_list) != NULL) \
474 goto finish; \
475 } while (0)
476
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200477/**
478 * kmod_module_new_from_lookup:
479 * @ctx: kmod library context
480 * @given_alias: alias to look for
481 * @list: an empty list where to save the list of modules matching
482 * @given_alias
483 *
484 * Create a new list of kmod modules using an alias or module name and lookup
485 * libkmod's configuration files and indexes in order to find the module.
486 * Once it's found in one of the places, it stops searching and create the
487 * list of modules that is saved in @list.
488 *
489 * The search order is: 1. aliases in configuration file; 2. module names in
490 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
491 * in modules.alias index.
492 *
493 * The initial refcount is 1, and needs to be decremented to release the
494 * resources of the kmod_module. The returned @list must be released by
495 * calling kmod_module_unref_list(). Since libkmod keeps track of all
496 * kmod_modules created, they are all released upon @ctx destruction too. Do
497 * not unref @ctx before all the desired operations with the returned list are
498 * completed.
499 *
500 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
501 * methods failed, which is basically due to memory allocation fail. If module
502 * is not found, it still returns 0, but @list is an empty list.
503 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200504KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200505 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200506 struct kmod_list **list)
507{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200508 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200509 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200510
Lucas De Marchi4308b172011-12-13 10:26:04 -0200511 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200512 return -ENOENT;
513
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200514 if (list == NULL || *list != NULL) {
515 ERR(ctx, "An empty list is needed to create lookup\n");
516 return -ENOSYS;
517 }
518
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200519 if (alias_normalize(given_alias, alias, NULL) < 0) {
520 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200521 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200522 }
523
524 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200525
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200526 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200527 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200528 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200529
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200530 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200531 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
532 CHECK_ERR_AND_FINISH(err, fail, list, finish);
533
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200534 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200535 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
536 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200537
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200538 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200539 err = kmod_lookup_alias_from_commands(ctx, alias, list);
540 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200541
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200542 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200543 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
544 CHECK_ERR_AND_FINISH(err, fail, list, finish);
545
Lucas De Marchi38052742012-02-16 20:43:16 -0200546 DBG(ctx, "lookup modules.builtin %s\n", alias);
547 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
548 CHECK_ERR_AND_FINISH(err, fail, list, finish);
549
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200550finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200551 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200552 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200553fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200554 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200555 kmod_module_unref_list(*list);
556 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200557 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200558}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200559#undef CHECK_ERR_AND_FINISH
560
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200561/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200562 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200563 * @list: list of kmod modules
564 *
565 * Drop a reference of each kmod module in @list and releases the resources
566 * taken by the list itself.
567 *
568 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
569 * returns the passed @mod with its refcount decremented.
570 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200571KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
572{
573 for (; list != NULL; list = kmod_list_remove(list))
574 kmod_module_unref(list->data);
575
576 return 0;
577}
578
Lucas De Marchi0d467432011-12-31 11:15:52 -0200579/**
580 * kmod_module_get_filtered_blacklist:
581 * @ctx: kmod library context
582 * @input: list of kmod_module to be filtered with blacklist
583 * @output: where to save the new list
584 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200585 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500586 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200587 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500588 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200589 *
590 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
591 * list.
592 */
593KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
594 const struct kmod_list *input,
595 struct kmod_list **output)
596{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500597 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200598}
599
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200600static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
601{
602 if (!mod->init.dep) {
603 /* lazy init */
604 char *line = kmod_search_moddep(mod->ctx, mod->name);
605
606 if (line == NULL)
607 return NULL;
608
609 kmod_module_parse_depline((struct kmod_module *)mod, line);
610 free(line);
611
612 if (!mod->init.dep)
613 return NULL;
614 }
615
616 return mod->dep;
617}
618
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200619/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200620 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200621 * @mod: kmod module
622 *
623 * Search the modules.dep index to find the dependencies of the given @mod.
624 * The result is cached in @mod, so subsequent calls to this function will
625 * return the already searched list of modules.
626 *
627 * Returns: NULL on failure or if there are any dependencies. Otherwise it
628 * returns a list of kmod modules that can be released by calling
629 * kmod_module_unref_list().
630 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200631KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200632{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200633 struct kmod_list *l, *l_new, *list_new = NULL;
634
635 if (mod == NULL)
636 return NULL;
637
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200638 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200639
640 kmod_list_foreach(l, mod->dep) {
641 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
642 if (l_new == NULL) {
643 kmod_module_unref(l->data);
644 goto fail;
645 }
646
647 list_new = l_new;
648 }
649
650 return list_new;
651
652fail:
653 ERR(mod->ctx, "out of memory\n");
654 kmod_module_unref_list(list_new);
655 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200656}
657
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200658/**
659 * kmod_module_get_module:
660 * @entry: an entry in a list of kmod modules.
661 *
662 * Get the kmod module of this @entry in the list, increasing its refcount.
663 * After it's used, unref it. Since the refcount is incremented upon return,
664 * you still have to call kmod_module_unref_list() to release the list of kmod
665 * modules.
666 *
667 * Returns: NULL on failure or the kmod_module contained in this list entry
668 * with its refcount incremented.
669 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200670KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200671{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200672 if (entry == NULL)
673 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200674
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200675 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200676}
677
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200678/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200679 * kmod_module_get_name:
680 * @mod: kmod module
681 *
682 * Get the name of this kmod module. Name is always available, independently
683 * if it was created by kmod_module_new_from_name() or another function and
684 * it's always normalized (dashes are replaced with underscores).
685 *
686 * Returns: the name of this kmod module.
687 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200688KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200689{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200690 if (mod == NULL)
691 return NULL;
692
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200693 return mod->name;
694}
695
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200696/**
697 * kmod_module_get_path:
698 * @mod: kmod module
699 *
700 * Get the path of this kmod module. If this kmod module was not created by
701 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200702 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200703 *
704 * Returns: the path of this kmod module or NULL if such information is not
705 * available.
706 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200707KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200708{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200709 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200710
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200711 if (mod == NULL)
712 return NULL;
713
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200714 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200715
Lucas De Marchie005fac2011-12-08 10:42:34 -0200716 if (mod->path != NULL)
717 return mod->path;
718 if (mod->init.dep)
719 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200720
Lucas De Marchie005fac2011-12-08 10:42:34 -0200721 /* lazy init */
722 line = kmod_search_moddep(mod->ctx, mod->name);
723 if (line == NULL)
724 return NULL;
725
726 kmod_module_parse_depline((struct kmod_module *) mod, line);
727 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200728
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200729 return mod->path;
730}
731
732
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200733extern long delete_module(const char *name, unsigned int flags);
734
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200735/**
736 * kmod_module_remove_module:
737 * @mod: kmod module
738 * @flags: flags to pass to Linux kernel when removing the module
739 *
740 * Remove a module from Linux kernel.
741 *
742 * Returns: 0 on success or < 0 on failure.
743 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200744KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
745 unsigned int flags)
746{
747 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200748
749 if (mod == NULL)
750 return -ENOENT;
751
752 /* Filter out other flags */
753 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
754
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200755 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200756 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200757 err = -errno;
758 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200759 }
760
Lucas De Marchiba998b92012-01-11 00:08:14 -0200761 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200762}
763
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200764extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200765
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200766/**
767 * kmod_module_insert_module:
768 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200769 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
770 * behavior of this function.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200771 * @options: module's options to pass to Linux Kernel.
772 *
773 * Insert a module in Linux kernel. It opens the file pointed by @mod,
774 * mmap'ing it and passing to kernel.
775 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200776 * Returns: 0 on success or < 0 on failure. If module is already loaded it
777 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200778 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200779KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200780 unsigned int flags,
781 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200782{
783 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200784 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200785 off_t size;
786 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200787 struct kmod_elf *elf = NULL;
788 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200789 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200790
791 if (mod == NULL)
792 return -ENOENT;
793
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200794 path = kmod_module_get_path(mod);
795 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500796 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200797 return -ENOSYS;
798 }
799
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200800 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200801 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200802 err = -errno;
803 return err;
804 }
805
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200806 size = kmod_file_get_size(file);
807 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200808
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200809 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
810 elf = kmod_elf_new(mem, size);
811 if (elf == NULL) {
812 err = -errno;
813 goto elf_failed;
814 }
815
816 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
817 err = kmod_elf_strip_section(elf, "__versions");
818 if (err < 0)
819 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
820 }
821
822 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
823 err = kmod_elf_strip_vermagic(elf);
824 if (err < 0)
825 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
826 }
827
828 mem = kmod_elf_get_memory(elf);
829 }
830
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200831 err = init_module(mem, size, args);
Lucas De Marchibbf59322011-12-30 14:13:33 -0200832 if (err < 0) {
833 err = -errno;
834 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
835 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200836
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200837 if (elf != NULL)
838 kmod_elf_unref(elf);
839elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200840 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200841
842 return err;
843}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200844
Lucas De Marchiddbda022011-12-27 11:40:10 -0200845static bool module_is_blacklisted(struct kmod_module *mod)
846{
847 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300848 const struct kmod_config *config = kmod_get_config(ctx);
849 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200850 const struct kmod_list *l;
851
852 kmod_list_foreach(l, bl) {
853 const char *modname = kmod_blacklist_get_modname(l);
854
855 if (streq(modname, mod->name))
856 return true;
857 }
858
859 return false;
860}
861
Dave Reisnerd80b1032012-02-24 10:05:11 -0500862/**
863 * kmod_module_apply_filter
864 * @ctx: kmod library context
865 * @filter_type: bitmask to filter modules on
866 * @input: list of kmod_module to be filtered
867 * @output: where to save the new list
868 *
869 * Given a list @input, this function filter it out by the filter mask
870 * and save it in @output.
871 *
872 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
873 * list.
874 */
875KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
876 enum kmod_filter filter_type,
877 const struct kmod_list *input,
878 struct kmod_list **output)
879{
880 const struct kmod_list *li;
881
882 if (ctx == NULL || output == NULL)
883 return -ENOENT;
884
885 *output = NULL;
886 if (input == NULL)
887 return 0;
888
889 kmod_list_foreach(li, input) {
890 struct kmod_module *mod = li->data;
891 struct kmod_list *node;
892
893 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
894 module_is_blacklisted(mod))
895 continue;
896
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500897 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500898 continue;
899
900 node = kmod_list_append(*output, mod);
901 if (node == NULL)
902 goto fail;
903
904 *output = node;
905 kmod_module_ref(mod);
906 }
907
908 return 0;
909
910fail:
911 kmod_module_unref_list(*output);
912 *output = NULL;
913 return -ENOMEM;
914}
915
Lucas De Marchiddbda022011-12-27 11:40:10 -0200916static int command_do(struct kmod_module *mod, const char *type,
917 const char *cmd)
918{
919 const char *modname = kmod_module_get_name(mod);
920 int err;
921
922 DBG(mod->ctx, "%s %s\n", type, cmd);
923
924 setenv("MODPROBE_MODULE", modname, 1);
925 err = system(cmd);
926 unsetenv("MODPROBE_MODULE");
927
928 if (err == -1 || WEXITSTATUS(err)) {
929 ERR(mod->ctx, "Error running %s command for %s\n",
930 type, modname);
931 if (err != -1)
932 err = -WEXITSTATUS(err);
933 }
934
935 return err;
936}
937
Lucas De Marchib1a51252012-01-29 01:49:09 -0200938struct probe_insert_cb {
939 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
940 void *data;
941};
942
Lucas De Marchiddbda022011-12-27 11:40:10 -0200943static int module_do_install_commands(struct kmod_module *mod,
944 const char *options,
945 struct probe_insert_cb *cb)
946{
947 const char *command = kmod_module_get_install_commands(mod);
948 char *p, *cmd;
949 int err;
950 size_t cmdlen, options_len, varlen;
951
952 assert(command);
953
954 if (options == NULL)
955 options = "";
956
957 options_len = strlen(options);
958 cmdlen = strlen(command);
959 varlen = sizeof("$CMDLINE_OPTS") - 1;
960
961 cmd = memdup(command, cmdlen + 1);
962 if (cmd == NULL)
963 return -ENOMEM;
964
965 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
966 size_t prefixlen = p - cmd;
967 size_t suffixlen = cmdlen - prefixlen - varlen;
968 size_t slen = cmdlen - varlen + options_len;
969 char *suffix = p + varlen;
970 char *s = malloc(slen + 1);
971 if (s == NULL) {
972 free(cmd);
973 return -ENOMEM;
974 }
975 memcpy(s, cmd, p - cmd);
976 memcpy(s + prefixlen, options, options_len);
977 memcpy(s + prefixlen + options_len, suffix, suffixlen);
978 s[slen] = '\0';
979
980 free(cmd);
981 cmd = s;
982 cmdlen = slen;
983 }
984
985 if (cb->run_install != NULL)
986 err = cb->run_install(mod, cmd, cb->data);
987 else
988 err = command_do(mod, "install", cmd);
989
990 free(cmd);
991
992 return err;
993}
994
Lucas De Marchiddbda022011-12-27 11:40:10 -0200995static char *module_options_concat(const char *opt, const char *xopt)
996{
997 // TODO: we might need to check if xopt overrides options on opt
998 size_t optlen = opt == NULL ? 0 : strlen(opt);
999 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1000 char *r;
1001
1002 if (optlen == 0 && xoptlen == 0)
1003 return NULL;
1004
1005 r = malloc(optlen + xoptlen + 2);
1006
1007 if (opt != NULL) {
1008 memcpy(r, opt, optlen);
1009 r[optlen] = ' ';
1010 optlen++;
1011 }
1012
1013 if (xopt != NULL)
1014 memcpy(r + optlen, xopt, xoptlen);
1015
1016 r[optlen + xoptlen] = '\0';
1017
1018 return r;
1019}
1020
Lucas De Marchib1a51252012-01-29 01:49:09 -02001021static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001022 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001023 struct kmod_list **list);
1024
1025/* re-entrant */
1026static int __kmod_module_fill_softdep(struct kmod_module *mod,
1027 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001028{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001029 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001030 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001031
1032 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001033 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001034 ERR(mod->ctx, "could not get softdep: %s\n",
1035 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001036 goto fail;
1037 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001038
Lucas De Marchib1a51252012-01-29 01:49:09 -02001039 kmod_list_foreach(l, pre) {
1040 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001041 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001042 if (err < 0)
1043 goto fail;
1044 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001045
Lucas De Marchib1a51252012-01-29 01:49:09 -02001046 l = kmod_list_append(*list, kmod_module_ref(mod));
1047 if (l == NULL) {
1048 kmod_module_unref(mod);
1049 err = -ENOMEM;
1050 goto fail;
1051 }
1052 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001053 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001054
Lucas De Marchib1a51252012-01-29 01:49:09 -02001055 kmod_list_foreach(l, post) {
1056 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001057 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001058 if (err < 0)
1059 goto fail;
1060 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001061
Lucas De Marchib1a51252012-01-29 01:49:09 -02001062fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001063 kmod_module_unref_list(pre);
1064 kmod_module_unref_list(post);
1065
1066 return err;
1067}
1068
Lucas De Marchib1a51252012-01-29 01:49:09 -02001069/* re-entrant */
1070static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001071 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001072 struct kmod_list **list)
1073{
1074 struct kmod_list *dep, *l;
1075 int err = 0;
1076
1077 if (mod->visited) {
1078 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1079 mod->name);
1080 return 0;
1081 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001082 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001083
1084 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001085 kmod_list_foreach(l, dep) {
1086 struct kmod_module *m = l->data;
1087 err = __kmod_module_fill_softdep(m, list);
1088 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001089 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001090 }
1091
Lucas De Marchi89e92482012-01-29 02:35:46 -02001092 if (ignorecmd) {
1093 l = kmod_list_append(*list, kmod_module_ref(mod));
1094 if (l == NULL) {
1095 kmod_module_unref(mod);
1096 err = -ENOMEM;
1097 goto finish;
1098 }
1099 *list = l;
1100 mod->ignorecmd = true;
1101 } else
1102 err = __kmod_module_fill_softdep(mod, list);
1103
Lucas De Marchib1a51252012-01-29 01:49:09 -02001104finish:
1105 kmod_module_unref_list(dep);
1106 return err;
1107}
1108
1109static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001110 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001111 struct kmod_list **list)
1112{
1113 int err;
1114
1115 assert(mod != NULL);
1116 assert(list != NULL && *list == NULL);
1117
1118 /*
1119 * Make sure we don't get screwed by previous calls to this function
1120 */
1121 kmod_set_modules_visited(mod->ctx, false);
1122
Lucas De Marchi89e92482012-01-29 02:35:46 -02001123 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001124 if (err < 0) {
1125 kmod_module_unref_list(*list);
1126 *list = NULL;
1127 }
1128
1129 return err;
1130}
1131
Lucas De Marchiddbda022011-12-27 11:40:10 -02001132/**
1133 * kmod_module_probe_insert_module:
1134 * @mod: kmod module
1135 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1136 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001137 * @extra_options: module's options to pass to Linux Kernel. It applies only
1138 * to @mod, not to its dependencies.
1139 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001140 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001141 * @print_action: function to call with the action being taken (install or
1142 * insmod). It's useful for tools like modprobe when running with verbose
1143 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001144 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001145 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001146 * install commands and applying blacklist.
1147 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001148 * If @run_install is NULL, this function will fork and exec by calling
1149 * system(3). Don't pass a NULL argument in @run_install if your binary is
1150 * setuid/setgid (see warning in system(3)). If you need control over the
1151 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001152 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001153 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1154 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001155 */
1156KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1157 unsigned int flags, const char *extra_options,
1158 int (*run_install)(struct kmod_module *m,
1159 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001160 const void *data,
1161 void (*print_action)(struct kmod_module *m,
1162 bool install,
1163 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001164{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001165 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001166 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001167 int err;
1168
1169 if (mod == NULL)
1170 return -ENOENT;
1171
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001172 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1173 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001174 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001175 return -EEXIST;
1176 else
1177 return 0;
1178 }
1179
Lucas De Marchi68820172012-08-17 09:38:05 -03001180 /*
1181 * Ugly assignement + check. We need to check if we were told to check
1182 * blacklist and also return the reason why we failed.
1183 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1184 * module is an alias, so we also need to check it
1185 */
1186 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1187 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1188 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001189 if (module_is_blacklisted(mod))
1190 return err;
1191 }
1192
Lucas De Marchi89e92482012-01-29 02:35:46 -02001193 err = kmod_module_get_probe_list(mod,
1194 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001195 if (err < 0)
1196 return err;
1197
1198 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1199 struct kmod_list *filtered = NULL;
1200
Dave Reisnerd80b1032012-02-24 10:05:11 -05001201 err = kmod_module_apply_filter(mod->ctx,
1202 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001203 if (err < 0)
1204 return err;
1205
1206 kmod_module_unref_list(list);
1207 if (filtered == NULL)
1208 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1209
1210 list = filtered;
1211 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001212
1213 cb.run_install = run_install;
1214 cb.data = (void *) data;
1215
Lucas De Marchib1a51252012-01-29 01:49:09 -02001216 kmod_list_foreach(l, list) {
1217 struct kmod_module *m = l->data;
1218 const char *moptions = kmod_module_get_options(m);
1219 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001220 char *options;
1221
1222 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1223 && module_is_inkernel(m)) {
1224 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1225 m->name);
1226 err = -EEXIST;
1227 goto finish_module;
1228 }
1229
1230 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001231 m == mod ? extra_options : NULL);
1232
Lucas De Marchi89e92482012-01-29 02:35:46 -02001233 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001234 if (print_action != NULL)
1235 print_action(m, true, options ?: "");
1236
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001237 if (!(flags & KMOD_PROBE_DRY_RUN))
1238 err = module_do_install_commands(m, options,
1239 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001240 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001241 if (print_action != NULL)
1242 print_action(m, false, options ?: "");
1243
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001244 if (!(flags & KMOD_PROBE_DRY_RUN))
1245 err = kmod_module_insert_module(m, flags,
1246 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001247 }
1248
1249 free(options);
1250
Lucas De Marchiabd55572012-02-19 04:20:30 -02001251finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001252 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001253 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001254 * already loaded and the module being loaded is not a softdep
1255 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001256 *
1257 * We need to check here because of race conditions. We
1258 * checked first if module was already loaded but it may have
1259 * been loaded between the check and the moment we try to
1260 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001261 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001262 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001263 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001264 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001265
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001266 if (err == -EEXIST)
1267 err = 0;
1268 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001269 break;
1270 }
1271
1272 kmod_module_unref_list(list);
1273 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001274}
1275
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001276/**
1277 * kmod_module_get_options:
1278 * @mod: kmod module
1279 *
1280 * Get options of this kmod module. Options come from the configuration file
1281 * and are cached in @mod. The first call to this function will search for
1282 * this module in configuration and subsequent calls return the cached string.
1283 *
1284 * Returns: a string with all the options separated by spaces. This string is
1285 * owned by @mod, do not free it.
1286 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001287KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1288{
1289 if (mod == NULL)
1290 return NULL;
1291
1292 if (!mod->init.options) {
1293 /* lazy init */
1294 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001295 const struct kmod_list *l;
1296 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001297 char *opts = NULL;
1298 size_t optslen = 0;
1299
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001300 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001301
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001302 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001303 const char *modname = kmod_option_get_modname(l);
1304 const char *str;
1305 size_t len;
1306 void *tmp;
1307
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001308 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1309 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1310 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001311 continue;
1312
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001313 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 -02001314 str = kmod_option_get_options(l);
1315 len = strlen(str);
1316 if (len < 1)
1317 continue;
1318
1319 tmp = realloc(opts, optslen + len + 2);
1320 if (tmp == NULL) {
1321 free(opts);
1322 goto failed;
1323 }
1324
1325 opts = tmp;
1326
1327 if (optslen > 0) {
1328 opts[optslen] = ' ';
1329 optslen++;
1330 }
1331
1332 memcpy(opts + optslen, str, len);
1333 optslen += len;
1334 opts[optslen] = '\0';
1335 }
1336
1337 m->init.options = true;
1338 m->options = opts;
1339 }
1340
1341 return mod->options;
1342
1343failed:
1344 ERR(mod->ctx, "out of memory\n");
1345 return NULL;
1346}
1347
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001348/**
1349 * kmod_module_get_install_commands:
1350 * @mod: kmod module
1351 *
1352 * Get install commands for this kmod module. Install commands come from the
1353 * configuration file and are cached in @mod. The first call to this function
1354 * will search for this module in configuration and subsequent calls return
1355 * the cached string. The install commands are returned as they were in the
1356 * configuration, concatenated by ';'. No other processing is made in this
1357 * string.
1358 *
1359 * Returns: a string with all install commands separated by semicolons. This
1360 * string is owned by @mod, do not free it.
1361 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001362KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1363{
1364 if (mod == NULL)
1365 return NULL;
1366
1367 if (!mod->init.install_commands) {
1368 /* lazy init */
1369 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001370 const struct kmod_list *l;
1371 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001372
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001373 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001374
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001375 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001376 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001377
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001378 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001379 continue;
1380
Lucas De Marchi60f67602011-12-16 03:33:26 -02001381 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001382
Lucas De Marchi60f67602011-12-16 03:33:26 -02001383 /*
1384 * find only the first command, as modprobe from
1385 * module-init-tools does
1386 */
1387 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001388 }
1389
1390 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001391 }
1392
1393 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001394}
1395
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001396void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1397{
1398 mod->init.install_commands = true;
1399 mod->install_commands = cmd;
1400}
1401
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001402static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1403{
1404 struct kmod_list *ret = NULL;
1405 unsigned i;
1406
1407 for (i = 0; i < count; i++) {
1408 const char *depname = array[i];
1409 struct kmod_list *lst = NULL;
1410 int err;
1411
1412 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1413 if (err < 0) {
1414 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1415 continue;
1416 } else if (lst != NULL)
1417 ret = kmod_list_append_list(ret, lst);
1418 }
1419 return ret;
1420}
1421
1422/**
1423 * kmod_module_get_softdeps:
1424 * @mod: kmod module
1425 * @pre: where to save the list of preceding soft dependencies.
1426 * @post: where to save the list of post soft dependencies.
1427 *
1428 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001429 * from configuration file and are not cached in @mod because it may include
1430 * dependency cycles that would make we leak kmod_module. Any call
1431 * to this function will search for this module in configuration, allocate a
1432 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001433 *
1434 * Both @pre and @post are newly created list of kmod_module and
1435 * should be unreferenced with kmod_module_unref_list().
1436 *
1437 * Returns: 0 on success or < 0 otherwise.
1438 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001439KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1440 struct kmod_list **pre,
1441 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001442{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001443 const struct kmod_list *l;
1444 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001445
1446 if (mod == NULL || pre == NULL || post == NULL)
1447 return -ENOENT;
1448
1449 assert(*pre == NULL);
1450 assert(*post == NULL);
1451
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001452 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001453
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001454 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001455 const char *modname = kmod_softdep_get_name(l);
1456 const char * const *array;
1457 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001458
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001459 if (fnmatch(modname, mod->name, 0) != 0)
1460 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001461
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001462 array = kmod_softdep_get_pre(l, &count);
1463 *pre = lookup_softdep(mod->ctx, array, count);
1464 array = kmod_softdep_get_post(l, &count);
1465 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001466
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001467 /*
1468 * find only the first command, as modprobe from
1469 * module-init-tools does
1470 */
1471 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001472 }
1473
1474 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001475}
1476
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001477/**
1478 * kmod_module_get_remove_commands:
1479 * @mod: kmod module
1480 *
1481 * Get remove commands for this kmod module. Remove commands come from the
1482 * configuration file and are cached in @mod. The first call to this function
1483 * will search for this module in configuration and subsequent calls return
1484 * the cached string. The remove commands are returned as they were in the
1485 * configuration, concatenated by ';'. No other processing is made in this
1486 * string.
1487 *
1488 * Returns: a string with all remove commands separated by semicolons. This
1489 * string is owned by @mod, do not free it.
1490 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001491KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1492{
1493 if (mod == NULL)
1494 return NULL;
1495
1496 if (!mod->init.remove_commands) {
1497 /* lazy init */
1498 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001499 const struct kmod_list *l;
1500 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001501
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001502 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001503
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001504 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001505 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001506
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001507 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001508 continue;
1509
Lucas De Marchi60f67602011-12-16 03:33:26 -02001510 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001511
Lucas De Marchi60f67602011-12-16 03:33:26 -02001512 /*
1513 * find only the first command, as modprobe from
1514 * module-init-tools does
1515 */
1516 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001517 }
1518
1519 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001520 }
1521
1522 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001523}
1524
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001525void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1526{
1527 mod->init.remove_commands = true;
1528 mod->remove_commands = cmd;
1529}
1530
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001531/**
1532 * SECTION:libkmod-loaded
1533 * @short_description: currently loaded modules
1534 *
1535 * Information about currently loaded modules, as reported by Linux kernel.
1536 * These information are not cached by libkmod and are always read from /sys
1537 * and /proc/modules.
1538 */
1539
1540/**
1541 * kmod_module_new_from_loaded:
1542 * @ctx: kmod library context
1543 * @list: where to save the list of loaded modules
1544 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001545 * Create a new list of kmod modules with all modules currently loaded in
1546 * kernel. It uses /proc/modules to get the names of loaded modules and to
1547 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1548 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001549 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001550 * The initial refcount is 1, and needs to be decremented to release the
1551 * resources of the kmod_module. The returned @list must be released by
1552 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1553 * kmod_modules created, they are all released upon @ctx destruction too. Do
1554 * not unref @ctx before all the desired operations with the returned list are
1555 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001556 *
1557 * Returns: 0 on success or < 0 on error.
1558 */
1559KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1560 struct kmod_list **list)
1561{
1562 struct kmod_list *l = NULL;
1563 FILE *fp;
1564 char line[4096];
1565
1566 if (ctx == NULL || list == NULL)
1567 return -ENOENT;
1568
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001569 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001570 if (fp == NULL) {
1571 int err = -errno;
1572 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1573 return err;
1574 }
1575
1576 while (fgets(line, sizeof(line), fp)) {
1577 struct kmod_module *m;
1578 struct kmod_list *node;
1579 int err;
1580 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1581
1582 err = kmod_module_new_from_name(ctx, name, &m);
1583 if (err < 0) {
1584 ERR(ctx, "could not get module from name '%s': %s\n",
1585 name, strerror(-err));
1586 continue;
1587 }
1588
1589 node = kmod_list_append(l, m);
1590 if (node)
1591 l = node;
1592 else {
1593 ERR(ctx, "out of memory\n");
1594 kmod_module_unref(m);
1595 }
1596 }
1597
1598 fclose(fp);
1599 *list = l;
1600
1601 return 0;
1602}
1603
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001604/**
1605 * kmod_module_initstate_str:
1606 * @state: the state as returned by kmod_module_get_initstate()
1607 *
1608 * Translate a initstate to a string.
1609 *
1610 * Returns: the string associated to the @state. This string is statically
1611 * allocated, do not free it.
1612 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001613KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1614{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001615 switch (state) {
1616 case KMOD_MODULE_BUILTIN:
1617 return "builtin";
1618 case KMOD_MODULE_LIVE:
1619 return "live";
1620 case KMOD_MODULE_COMING:
1621 return "coming";
1622 case KMOD_MODULE_GOING:
1623 return "going";
1624 default:
1625 return NULL;
1626 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001627}
1628
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001629/**
1630 * kmod_module_get_initstate:
1631 * @mod: kmod module
1632 *
1633 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1634 * /sys filesystem.
1635 *
1636 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1637 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001638KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1639{
1640 char path[PATH_MAX], buf[32];
1641 int fd, err, pathlen;
1642
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001643 if (mod == NULL)
1644 return -ENOENT;
1645
Lucas De Marchi38052742012-02-16 20:43:16 -02001646 if (mod->builtin)
1647 return KMOD_MODULE_BUILTIN;
1648
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001649 pathlen = snprintf(path, sizeof(path),
1650 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001651 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001652 if (fd < 0) {
1653 err = -errno;
1654
Lucas De Marchiddbda022011-12-27 11:40:10 -02001655 DBG(mod->ctx, "could not open '%s': %s\n",
1656 path, strerror(-err));
1657
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001658 if (pathlen > (int)sizeof("/initstate") - 1) {
1659 struct stat st;
1660 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1661 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1662 return KMOD_MODULE_BUILTIN;
1663 }
1664
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001665 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001666 path, strerror(-err));
1667 return err;
1668 }
1669
1670 err = read_str_safe(fd, buf, sizeof(buf));
1671 close(fd);
1672 if (err < 0) {
1673 ERR(mod->ctx, "could not read from '%s': %s\n",
1674 path, strerror(-err));
1675 return err;
1676 }
1677
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001678 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001679 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001680 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001681 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001682 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001683 return KMOD_MODULE_GOING;
1684
1685 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1686 return -EINVAL;
1687}
1688
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001689/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001690 * kmod_module_get_size:
1691 * @mod: kmod module
1692 *
Dave Reisner486f9012012-06-28 09:09:48 -04001693 * Get the size of this kmod module as returned by Linux kernel. If supported,
1694 * the size is read from the coresize attribute in /sys/module. For older
1695 * kernels, this falls back on /proc/modules and searches for the specified
1696 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001697 *
1698 * Returns: the size of this kmod module.
1699 */
1700KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1701{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001702 FILE *fp;
1703 char line[4096];
1704 int lineno = 0;
1705 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001706 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001707
1708 if (mod == NULL)
1709 return -ENOENT;
1710
Dave Reisner486f9012012-06-28 09:09:48 -04001711 /* try to open the module dir in /sys. If this fails, don't
1712 * bother trying to find the size as we know the module isn't
1713 * loaded.
1714 */
1715 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1716 dfd = open(line, O_RDONLY);
1717 if (dfd < 0)
1718 return -errno;
1719
1720 /* available as of linux 3.3.x */
1721 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1722 if (cfd >= 0) {
1723 if (read_str_long(cfd, &size, 10) < 0)
1724 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1725 close(cfd);
1726 goto done;
1727 }
1728
1729 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001730 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001731 if (fp == NULL) {
1732 int err = -errno;
1733 ERR(mod->ctx,
1734 "could not open /proc/modules: %s\n", strerror(errno));
1735 return err;
1736 }
1737
1738 while (fgets(line, sizeof(line), fp)) {
1739 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1740 long value;
1741
1742 lineno++;
1743 if (tok == NULL || !streq(tok, mod->name))
1744 continue;
1745
1746 tok = strtok_r(NULL, " \t", &saveptr);
1747 if (tok == NULL) {
1748 ERR(mod->ctx,
1749 "invalid line format at /proc/modules:%d\n", lineno);
1750 break;
1751 }
1752
1753 value = strtol(tok, &endptr, 10);
1754 if (endptr == tok || *endptr != '\0') {
1755 ERR(mod->ctx,
1756 "invalid line format at /proc/modules:%d\n", lineno);
1757 break;
1758 }
1759
1760 size = value;
1761 break;
1762 }
1763 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001764
1765done:
1766 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001767 return size;
1768}
1769
1770/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001771 * kmod_module_get_refcnt:
1772 * @mod: kmod module
1773 *
1774 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1775 * /sys filesystem.
1776 *
1777 * Returns: 0 on success or < 0 on failure.
1778 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001779KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1780{
1781 char path[PATH_MAX];
1782 long refcnt;
1783 int fd, err;
1784
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001785 if (mod == NULL)
1786 return -ENOENT;
1787
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001788 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001789 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001790 if (fd < 0) {
1791 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001792 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001793 path, strerror(errno));
1794 return err;
1795 }
1796
1797 err = read_str_long(fd, &refcnt, 10);
1798 close(fd);
1799 if (err < 0) {
1800 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1801 path, strerror(-err));
1802 return err;
1803 }
1804
1805 return (int)refcnt;
1806}
1807
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001808/**
1809 * kmod_module_get_holders:
1810 * @mod: kmod module
1811 *
1812 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1813 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1814 *
1815 * Returns: a new list of kmod modules on success or NULL on failure.
1816 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001817KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1818{
1819 char dname[PATH_MAX];
1820 struct kmod_list *list = NULL;
1821 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001822
1823 if (mod == NULL)
1824 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001825
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001826 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1827
1828 d = opendir(dname);
1829 if (d == NULL) {
1830 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001831 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001832 return NULL;
1833 }
1834
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001835 for (;;) {
1836 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001837 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001838 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001839 int err;
1840
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001841 err = readdir_r(d, &de, &entp);
1842 if (err != 0) {
1843 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1844 mod->name, strerror(-err));
1845 goto fail;
1846 }
1847
1848 if (entp == NULL)
1849 break;
1850
1851 if (de.d_name[0] == '.') {
1852 if (de.d_name[1] == '\0' ||
1853 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001854 continue;
1855 }
1856
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001857 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001858 if (err < 0) {
1859 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001860 de.d_name, strerror(-err));
1861 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001862 }
1863
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001864 l = kmod_list_append(list, holder);
1865 if (l != NULL) {
1866 list = l;
1867 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001868 ERR(mod->ctx, "out of memory\n");
1869 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001870 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001871 }
1872 }
1873
1874 closedir(d);
1875 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001876
1877fail:
1878 closedir(d);
1879 kmod_module_unref_list(list);
1880 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001881}
1882
1883struct kmod_module_section {
1884 unsigned long address;
1885 char name[];
1886};
1887
1888static void kmod_module_section_free(struct kmod_module_section *section)
1889{
1890 free(section);
1891}
1892
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001893/**
1894 * kmod_module_get_sections:
1895 * @mod: kmod module
1896 *
1897 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1898 * structure contained in this list is internal to libkmod and their fields
1899 * can be obtained by calling kmod_module_section_get_name() and
1900 * kmod_module_section_get_address().
1901 *
1902 * After use, free the @list by calling kmod_module_section_free_list().
1903 *
1904 * Returns: a new list of kmod module sections on success or NULL on failure.
1905 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001906KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1907{
1908 char dname[PATH_MAX];
1909 struct kmod_list *list = NULL;
1910 DIR *d;
1911 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001912
1913 if (mod == NULL)
1914 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001915
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001916 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1917
1918 d = opendir(dname);
1919 if (d == NULL) {
1920 ERR(mod->ctx, "could not open '%s': %s\n",
1921 dname, strerror(errno));
1922 return NULL;
1923 }
1924
1925 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001926
1927 for (;;) {
1928 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001929 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001930 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001931 unsigned long address;
1932 size_t namesz;
1933 int fd, err;
1934
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001935 err = readdir_r(d, &de, &entp);
1936 if (err != 0) {
1937 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1938 mod->name, strerror(-err));
1939 goto fail;
1940 }
1941
1942 if (de.d_name[0] == '.') {
1943 if (de.d_name[1] == '\0' ||
1944 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001945 continue;
1946 }
1947
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001948 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001949 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001950 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1951 dname, de.d_name);
1952 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001953 }
1954
1955 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001956 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001957 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001958 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1959 dname, de.d_name);
1960 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001961 }
1962
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001963 namesz = strlen(de.d_name) + 1;
1964 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001965
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001966 if (section == NULL) {
1967 ERR(mod->ctx, "out of memory\n");
1968 goto fail;
1969 }
1970
1971 section->address = address;
1972 memcpy(section->name, de.d_name, namesz);
1973
1974 l = kmod_list_append(list, section);
1975 if (l != NULL) {
1976 list = l;
1977 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001978 ERR(mod->ctx, "out of memory\n");
1979 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001980 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001981 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001982 }
1983
1984 closedir(d);
1985 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001986
1987fail:
1988 closedir(d);
1989 kmod_module_unref_list(list);
1990 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001991}
1992
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001993/**
1994 * kmod_module_section_get_module_name:
1995 * @entry: a list entry representing a kmod module section
1996 *
1997 * Get the name of a kmod module section.
1998 *
1999 * After use, free the @list by calling kmod_module_section_free_list().
2000 *
2001 * Returns: the name of this kmod module section on success or NULL on
2002 * failure. The string is owned by the section, do not free it.
2003 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002004KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2005{
2006 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002007
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002008 if (entry == NULL)
2009 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002010
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002011 section = entry->data;
2012 return section->name;
2013}
2014
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002015/**
2016 * kmod_module_section_get_address:
2017 * @entry: a list entry representing a kmod module section
2018 *
2019 * Get the address of a kmod module section.
2020 *
2021 * After use, free the @list by calling kmod_module_section_free_list().
2022 *
2023 * Returns: the address of this kmod module section on success or ULONG_MAX
2024 * on failure.
2025 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002026KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2027{
2028 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002029
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002030 if (entry == NULL)
2031 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002032
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002033 section = entry->data;
2034 return section->address;
2035}
2036
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002037/**
2038 * kmod_module_section_free_list:
2039 * @list: kmod module section list
2040 *
2041 * Release the resources taken by @list
2042 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002043KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2044{
2045 while (list) {
2046 kmod_module_section_free(list->data);
2047 list = kmod_list_remove(list);
2048 }
2049}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002050
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002051static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2052{
2053 if (mod->file == NULL) {
2054 const char *path = kmod_module_get_path(mod);
2055
2056 if (path == NULL) {
2057 errno = ENOENT;
2058 return NULL;
2059 }
2060
2061 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2062 path);
2063 if (mod->file == NULL)
2064 return NULL;
2065 }
2066
2067 return kmod_file_get_elf(mod->file);
2068}
2069
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002070struct kmod_module_info {
2071 char *key;
2072 char value[];
2073};
2074
2075static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2076{
2077 struct kmod_module_info *info;
2078
2079 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2080 if (info == NULL)
2081 return NULL;
2082
2083 info->key = (char *)info + sizeof(struct kmod_module_info)
2084 + valuelen + 1;
2085 memcpy(info->key, key, keylen);
2086 info->key[keylen] = '\0';
2087 memcpy(info->value, value, valuelen);
2088 info->value[valuelen] = '\0';
2089 return info;
2090}
2091
2092static void kmod_module_info_free(struct kmod_module_info *info)
2093{
2094 free(info);
2095}
2096
Michal Marekf64458c2013-01-16 10:18:17 +01002097static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2098{
2099 struct kmod_module_info *info;
2100 struct kmod_list *n;
2101
2102 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002103 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002104 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002105 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002106 if (n != NULL)
2107 *list = n;
2108 else
Michal Marekf64458c2013-01-16 10:18:17 +01002109 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002110 return n;
2111}
2112
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002113/**
2114 * kmod_module_get_info:
2115 * @mod: kmod module
2116 * @list: where to return list of module information. Use
2117 * kmod_module_info_get_key() and
2118 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002119 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002120 *
2121 * Get a list of entries in ELF section ".modinfo", these contain
2122 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002123 * values. If the module is signed (CONFIG_MODULE_SIG), information
2124 * about the module signature is included as well: signer,
2125 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002126 *
2127 * After use, free the @list by calling kmod_module_info_free_list().
2128 *
2129 * Returns: 0 on success or < 0 otherwise.
2130 */
2131KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2132{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002133 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002134 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002135 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002136 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002137
2138 if (mod == NULL || list == NULL)
2139 return -ENOENT;
2140
2141 assert(*list == NULL);
2142
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002143 elf = kmod_module_get_elf(mod);
2144 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002145 return -errno;
2146
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002147 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002148 if (count < 0)
2149 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002150
2151 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002152 struct kmod_list *n;
2153 const char *key, *value;
2154 size_t keylen, valuelen;
2155
2156 key = strings[i];
2157 value = strchr(key, '=');
2158 if (value == NULL) {
2159 keylen = strlen(key);
2160 valuelen = 0;
2161 } else {
2162 keylen = value - key;
2163 value++;
2164 valuelen = strlen(value);
2165 }
2166
Michal Marekf64458c2013-01-16 10:18:17 +01002167 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2168 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002169 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002170 }
Michal Marek8fe16812013-01-16 09:52:01 +01002171
2172 if (kmod_module_signature_info(mod->file, &sig_info)) {
2173 struct kmod_list *n;
2174 char *key_hex;
2175
2176 n = kmod_module_info_append(list, "signer", strlen("signer"),
2177 sig_info.signer, sig_info.signer_len);
2178 if (n == NULL)
2179 goto list_error;
2180 count++;
2181
2182 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2183 key_hex = malloc(sig_info.key_id_len * 3);
2184 if (key_hex == NULL)
2185 goto list_error;
2186 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2187 sprintf(key_hex + i * 3, "%02X",
2188 (unsigned char)sig_info.key_id[i]);
2189 if (i < (int)sig_info.key_id_len - 1)
2190 key_hex[i * 3 + 2] = ':';
2191 }
2192 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2193 key_hex, sig_info.key_id_len * 3 - 1);
2194 free(key_hex);
2195 if (n == NULL)
2196 goto list_error;
2197 count++;
2198
2199 n = kmod_module_info_append(list,
2200 "sig_hashalgo", strlen("sig_hashalgo"),
2201 sig_info.hash_algo, strlen(sig_info.hash_algo));
2202 if (n == NULL)
2203 goto list_error;
2204 count++;
2205
2206 /*
2207 * Omit sig_info.id_type and sig_info.algo for now, as these
2208 * are currently constant.
2209 */
2210 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002211 ret = count;
2212
2213list_error:
Michal Marek63339342013-01-16 17:51:57 +01002214 if (ret < 0) {
2215 kmod_module_info_free_list(*list);
2216 *list = NULL;
2217 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002218 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002219 return ret;
2220}
2221
2222/**
2223 * kmod_module_info_get_key:
2224 * @entry: a list entry representing a kmod module info
2225 *
2226 * Get the key of a kmod module info.
2227 *
2228 * Returns: the key of this kmod module info on success or NULL on
2229 * failure. The string is owned by the info, do not free it.
2230 */
2231KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2232{
2233 struct kmod_module_info *info;
2234
2235 if (entry == NULL)
2236 return NULL;
2237
2238 info = entry->data;
2239 return info->key;
2240}
2241
2242/**
2243 * kmod_module_info_get_value:
2244 * @entry: a list entry representing a kmod module info
2245 *
2246 * Get the value of a kmod module info.
2247 *
2248 * Returns: the value of this kmod module info on success or NULL on
2249 * failure. The string is owned by the info, do not free it.
2250 */
2251KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2252{
2253 struct kmod_module_info *info;
2254
2255 if (entry == NULL)
2256 return NULL;
2257
2258 info = entry->data;
2259 return info->value;
2260}
2261
2262/**
2263 * kmod_module_info_free_list:
2264 * @list: kmod module info list
2265 *
2266 * Release the resources taken by @list
2267 */
2268KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2269{
2270 while (list) {
2271 kmod_module_info_free(list->data);
2272 list = kmod_list_remove(list);
2273 }
2274}
2275
2276struct kmod_module_version {
2277 uint64_t crc;
2278 char symbol[];
2279};
2280
2281static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2282{
2283 struct kmod_module_version *mv;
2284 size_t symbollen = strlen(symbol) + 1;
2285
2286 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2287 if (mv == NULL)
2288 return NULL;
2289
2290 mv->crc = crc;
2291 memcpy(mv->symbol, symbol, symbollen);
2292 return mv;
2293}
2294
2295static void kmod_module_version_free(struct kmod_module_version *version)
2296{
2297 free(version);
2298}
2299
2300/**
2301 * kmod_module_get_versions:
2302 * @mod: kmod module
2303 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002304 * kmod_module_version_get_symbol() and
2305 * kmod_module_version_get_crc(). Release this list with
2306 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002307 *
2308 * Get a list of entries in ELF section "__versions".
2309 *
2310 * After use, free the @list by calling kmod_module_versions_free_list().
2311 *
2312 * Returns: 0 on success or < 0 otherwise.
2313 */
2314KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2315{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002316 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002317 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002318 int i, count, ret = 0;
2319
2320 if (mod == NULL || list == NULL)
2321 return -ENOENT;
2322
2323 assert(*list == NULL);
2324
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002325 elf = kmod_module_get_elf(mod);
2326 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002327 return -errno;
2328
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002329 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002330 if (count < 0)
2331 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002332
2333 for (i = 0; i < count; i++) {
2334 struct kmod_module_version *mv;
2335 struct kmod_list *n;
2336
2337 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2338 if (mv == NULL) {
2339 ret = -errno;
2340 kmod_module_versions_free_list(*list);
2341 *list = NULL;
2342 goto list_error;
2343 }
2344
2345 n = kmod_list_append(*list, mv);
2346 if (n != NULL)
2347 *list = n;
2348 else {
2349 kmod_module_version_free(mv);
2350 kmod_module_versions_free_list(*list);
2351 *list = NULL;
2352 ret = -ENOMEM;
2353 goto list_error;
2354 }
2355 }
2356 ret = count;
2357
2358list_error:
2359 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002360 return ret;
2361}
2362
2363/**
2364 * kmod_module_versions_get_symbol:
2365 * @entry: a list entry representing a kmod module versions
2366 *
2367 * Get the symbol of a kmod module versions.
2368 *
2369 * Returns: the symbol of this kmod module versions on success or NULL
2370 * on failure. The string is owned by the versions, do not free it.
2371 */
2372KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2373{
2374 struct kmod_module_version *version;
2375
2376 if (entry == NULL)
2377 return NULL;
2378
2379 version = entry->data;
2380 return version->symbol;
2381}
2382
2383/**
2384 * kmod_module_version_get_crc:
2385 * @entry: a list entry representing a kmod module version
2386 *
2387 * Get the crc of a kmod module version.
2388 *
2389 * Returns: the crc of this kmod module version on success or NULL on
2390 * failure. The string is owned by the version, do not free it.
2391 */
2392KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2393{
2394 struct kmod_module_version *version;
2395
2396 if (entry == NULL)
2397 return 0;
2398
2399 version = entry->data;
2400 return version->crc;
2401}
2402
2403/**
2404 * kmod_module_versions_free_list:
2405 * @list: kmod module versions list
2406 *
2407 * Release the resources taken by @list
2408 */
2409KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2410{
2411 while (list) {
2412 kmod_module_version_free(list->data);
2413 list = kmod_list_remove(list);
2414 }
2415}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002416
2417struct kmod_module_symbol {
2418 uint64_t crc;
2419 char symbol[];
2420};
2421
2422static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2423{
2424 struct kmod_module_symbol *mv;
2425 size_t symbollen = strlen(symbol) + 1;
2426
2427 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2428 if (mv == NULL)
2429 return NULL;
2430
2431 mv->crc = crc;
2432 memcpy(mv->symbol, symbol, symbollen);
2433 return mv;
2434}
2435
2436static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2437{
2438 free(symbol);
2439}
2440
2441/**
2442 * kmod_module_get_symbols:
2443 * @mod: kmod module
2444 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002445 * kmod_module_symbol_get_symbol() and
2446 * kmod_module_symbol_get_crc(). Release this list with
2447 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002448 *
2449 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2450 *
2451 * After use, free the @list by calling kmod_module_symbols_free_list().
2452 *
2453 * Returns: 0 on success or < 0 otherwise.
2454 */
2455KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2456{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002457 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002458 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002459 int i, count, ret = 0;
2460
2461 if (mod == NULL || list == NULL)
2462 return -ENOENT;
2463
2464 assert(*list == NULL);
2465
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002466 elf = kmod_module_get_elf(mod);
2467 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002468 return -errno;
2469
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002470 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002471 if (count < 0)
2472 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002473
2474 for (i = 0; i < count; i++) {
2475 struct kmod_module_symbol *mv;
2476 struct kmod_list *n;
2477
2478 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2479 if (mv == NULL) {
2480 ret = -errno;
2481 kmod_module_symbols_free_list(*list);
2482 *list = NULL;
2483 goto list_error;
2484 }
2485
2486 n = kmod_list_append(*list, mv);
2487 if (n != NULL)
2488 *list = n;
2489 else {
2490 kmod_module_symbol_free(mv);
2491 kmod_module_symbols_free_list(*list);
2492 *list = NULL;
2493 ret = -ENOMEM;
2494 goto list_error;
2495 }
2496 }
2497 ret = count;
2498
2499list_error:
2500 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002501 return ret;
2502}
2503
2504/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002505 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002506 * @entry: a list entry representing a kmod module symbols
2507 *
2508 * Get the symbol of a kmod module symbols.
2509 *
2510 * Returns: the symbol of this kmod module symbols on success or NULL
2511 * on failure. The string is owned by the symbols, do not free it.
2512 */
2513KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2514{
2515 struct kmod_module_symbol *symbol;
2516
2517 if (entry == NULL)
2518 return NULL;
2519
2520 symbol = entry->data;
2521 return symbol->symbol;
2522}
2523
2524/**
2525 * kmod_module_symbol_get_crc:
2526 * @entry: a list entry representing a kmod module symbol
2527 *
2528 * Get the crc of a kmod module symbol.
2529 *
2530 * Returns: the crc of this kmod module symbol on success or NULL on
2531 * failure. The string is owned by the symbol, do not free it.
2532 */
2533KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2534{
2535 struct kmod_module_symbol *symbol;
2536
2537 if (entry == NULL)
2538 return 0;
2539
2540 symbol = entry->data;
2541 return symbol->crc;
2542}
2543
2544/**
2545 * kmod_module_symbols_free_list:
2546 * @list: kmod module symbols list
2547 *
2548 * Release the resources taken by @list
2549 */
2550KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2551{
2552 while (list) {
2553 kmod_module_symbol_free(list->data);
2554 list = kmod_list_remove(list);
2555 }
2556}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002557
2558struct kmod_module_dependency_symbol {
2559 uint64_t crc;
2560 uint8_t bind;
2561 char symbol[];
2562};
2563
2564static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2565{
2566 struct kmod_module_dependency_symbol *mv;
2567 size_t symbollen = strlen(symbol) + 1;
2568
2569 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2570 if (mv == NULL)
2571 return NULL;
2572
2573 mv->crc = crc;
2574 mv->bind = bind;
2575 memcpy(mv->symbol, symbol, symbollen);
2576 return mv;
2577}
2578
2579static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2580{
2581 free(dependency_symbol);
2582}
2583
2584/**
2585 * kmod_module_get_dependency_symbols:
2586 * @mod: kmod module
2587 * @list: where to return list of module dependency_symbols. Use
2588 * kmod_module_dependency_symbol_get_symbol() and
2589 * kmod_module_dependency_symbol_get_crc(). Release this list with
2590 * kmod_module_dependency_symbols_free_list()
2591 *
2592 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2593 *
2594 * After use, free the @list by calling
2595 * kmod_module_dependency_symbols_free_list().
2596 *
2597 * Returns: 0 on success or < 0 otherwise.
2598 */
2599KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2600{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002601 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002602 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002603 int i, count, ret = 0;
2604
2605 if (mod == NULL || list == NULL)
2606 return -ENOENT;
2607
2608 assert(*list == NULL);
2609
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002610 elf = kmod_module_get_elf(mod);
2611 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002612 return -errno;
2613
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002614 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002615 if (count < 0)
2616 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002617
2618 for (i = 0; i < count; i++) {
2619 struct kmod_module_dependency_symbol *mv;
2620 struct kmod_list *n;
2621
2622 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2623 symbols[i].bind,
2624 symbols[i].symbol);
2625 if (mv == NULL) {
2626 ret = -errno;
2627 kmod_module_dependency_symbols_free_list(*list);
2628 *list = NULL;
2629 goto list_error;
2630 }
2631
2632 n = kmod_list_append(*list, mv);
2633 if (n != NULL)
2634 *list = n;
2635 else {
2636 kmod_module_dependency_symbol_free(mv);
2637 kmod_module_dependency_symbols_free_list(*list);
2638 *list = NULL;
2639 ret = -ENOMEM;
2640 goto list_error;
2641 }
2642 }
2643 ret = count;
2644
2645list_error:
2646 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002647 return ret;
2648}
2649
2650/**
2651 * kmod_module_dependency_symbol_get_symbol:
2652 * @entry: a list entry representing a kmod module dependency_symbols
2653 *
2654 * Get the dependency symbol of a kmod module
2655 *
2656 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2657 * on failure. The string is owned by the dependency_symbols, do not free it.
2658 */
2659KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2660{
2661 struct kmod_module_dependency_symbol *dependency_symbol;
2662
2663 if (entry == NULL)
2664 return NULL;
2665
2666 dependency_symbol = entry->data;
2667 return dependency_symbol->symbol;
2668}
2669
2670/**
2671 * kmod_module_dependency_symbol_get_crc:
2672 * @entry: a list entry representing a kmod module dependency_symbol
2673 *
2674 * Get the crc of a kmod module dependency_symbol.
2675 *
2676 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2677 * failure. The string is owned by the dependency_symbol, do not free it.
2678 */
2679KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2680{
2681 struct kmod_module_dependency_symbol *dependency_symbol;
2682
2683 if (entry == NULL)
2684 return 0;
2685
2686 dependency_symbol = entry->data;
2687 return dependency_symbol->crc;
2688}
2689
2690/**
2691 * kmod_module_dependency_symbol_get_bind:
2692 * @entry: a list entry representing a kmod module dependency_symbol
2693 *
2694 * Get the bind type of a kmod module dependency_symbol.
2695 *
2696 * Returns: the bind of this kmod module dependency_symbol on success
2697 * or < 0 on failure.
2698 */
2699KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2700{
2701 struct kmod_module_dependency_symbol *dependency_symbol;
2702
2703 if (entry == NULL)
2704 return 0;
2705
2706 dependency_symbol = entry->data;
2707 return dependency_symbol->bind;
2708}
2709
2710/**
2711 * kmod_module_dependency_symbols_free_list:
2712 * @list: kmod module dependency_symbols list
2713 *
2714 * Release the resources taken by @list
2715 */
2716KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2717{
2718 while (list) {
2719 kmod_module_dependency_symbol_free(list->data);
2720 list = kmod_list_remove(list);
2721 }
2722}