blob: 0d87ce1f9b9f76ee7b548985185b6b1889c4b7be [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchia66a6a92012-01-09 00:40:50 -02004 * Copyright (C) 2011-2012 ProFUSION embedded systems
Lucas De Marchi8f788d52011-11-25 01:22:56 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi8f788d52011-11-25 01:22:56 -020010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Lucas De Marchi7636e722011-12-01 17:56:03 -020021#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020022#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020031#include <limits.h>
32#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/mman.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020036#include <sys/wait.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020037#include <string.h>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020038#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020039
40#include "libkmod.h"
41#include "libkmod-private.h"
42
43/**
Lucas De Marchi66819512012-01-09 04:47:40 -020044 * SECTION:libkmod-module
45 * @short_description: operate on kernel modules
46 */
47
48/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020049 * kmod_module:
50 *
51 * Opaque object representing a module.
52 */
53struct kmod_module {
54 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020055 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020056 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020057 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020058 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020059 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020060 const char *install_commands; /* owned by kmod_config */
61 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020062 char *alias; /* only set if this module was created from an alias */
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
2097/**
2098 * kmod_module_get_info:
2099 * @mod: kmod module
2100 * @list: where to return list of module information. Use
2101 * kmod_module_info_get_key() and
2102 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002103 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002104 *
2105 * Get a list of entries in ELF section ".modinfo", these contain
2106 * alias, license, depends, vermagic and other keys with respective
2107 * values.
2108 *
2109 * After use, free the @list by calling kmod_module_info_free_list().
2110 *
2111 * Returns: 0 on success or < 0 otherwise.
2112 */
2113KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2114{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002115 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002116 char **strings;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002117 int i, count, ret = 0;
2118
2119 if (mod == NULL || list == NULL)
2120 return -ENOENT;
2121
2122 assert(*list == NULL);
2123
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002124 elf = kmod_module_get_elf(mod);
2125 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002126 return -errno;
2127
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002128 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002129 if (count < 0)
2130 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002131
2132 for (i = 0; i < count; i++) {
2133 struct kmod_module_info *info;
2134 struct kmod_list *n;
2135 const char *key, *value;
2136 size_t keylen, valuelen;
2137
2138 key = strings[i];
2139 value = strchr(key, '=');
2140 if (value == NULL) {
2141 keylen = strlen(key);
2142 valuelen = 0;
2143 } else {
2144 keylen = value - key;
2145 value++;
2146 valuelen = strlen(value);
2147 }
2148
2149 info = kmod_module_info_new(key, keylen, value, valuelen);
2150 if (info == NULL) {
2151 ret = -errno;
2152 kmod_module_info_free_list(*list);
2153 *list = NULL;
2154 goto list_error;
2155 }
2156
2157 n = kmod_list_append(*list, info);
2158 if (n != NULL)
2159 *list = n;
2160 else {
2161 kmod_module_info_free(info);
2162 kmod_module_info_free_list(*list);
2163 *list = NULL;
2164 ret = -ENOMEM;
2165 goto list_error;
2166 }
2167 }
2168 ret = count;
2169
2170list_error:
2171 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002172 return ret;
2173}
2174
2175/**
2176 * kmod_module_info_get_key:
2177 * @entry: a list entry representing a kmod module info
2178 *
2179 * Get the key of a kmod module info.
2180 *
2181 * Returns: the key of this kmod module info on success or NULL on
2182 * failure. The string is owned by the info, do not free it.
2183 */
2184KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2185{
2186 struct kmod_module_info *info;
2187
2188 if (entry == NULL)
2189 return NULL;
2190
2191 info = entry->data;
2192 return info->key;
2193}
2194
2195/**
2196 * kmod_module_info_get_value:
2197 * @entry: a list entry representing a kmod module info
2198 *
2199 * Get the value of a kmod module info.
2200 *
2201 * Returns: the value of this kmod module info on success or NULL on
2202 * failure. The string is owned by the info, do not free it.
2203 */
2204KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2205{
2206 struct kmod_module_info *info;
2207
2208 if (entry == NULL)
2209 return NULL;
2210
2211 info = entry->data;
2212 return info->value;
2213}
2214
2215/**
2216 * kmod_module_info_free_list:
2217 * @list: kmod module info list
2218 *
2219 * Release the resources taken by @list
2220 */
2221KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2222{
2223 while (list) {
2224 kmod_module_info_free(list->data);
2225 list = kmod_list_remove(list);
2226 }
2227}
2228
2229struct kmod_module_version {
2230 uint64_t crc;
2231 char symbol[];
2232};
2233
2234static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2235{
2236 struct kmod_module_version *mv;
2237 size_t symbollen = strlen(symbol) + 1;
2238
2239 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2240 if (mv == NULL)
2241 return NULL;
2242
2243 mv->crc = crc;
2244 memcpy(mv->symbol, symbol, symbollen);
2245 return mv;
2246}
2247
2248static void kmod_module_version_free(struct kmod_module_version *version)
2249{
2250 free(version);
2251}
2252
2253/**
2254 * kmod_module_get_versions:
2255 * @mod: kmod module
2256 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002257 * kmod_module_version_get_symbol() and
2258 * kmod_module_version_get_crc(). Release this list with
2259 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002260 *
2261 * Get a list of entries in ELF section "__versions".
2262 *
2263 * After use, free the @list by calling kmod_module_versions_free_list().
2264 *
2265 * Returns: 0 on success or < 0 otherwise.
2266 */
2267KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2268{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002269 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002270 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002271 int i, count, ret = 0;
2272
2273 if (mod == NULL || list == NULL)
2274 return -ENOENT;
2275
2276 assert(*list == NULL);
2277
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002278 elf = kmod_module_get_elf(mod);
2279 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002280 return -errno;
2281
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002282 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002283 if (count < 0)
2284 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002285
2286 for (i = 0; i < count; i++) {
2287 struct kmod_module_version *mv;
2288 struct kmod_list *n;
2289
2290 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2291 if (mv == NULL) {
2292 ret = -errno;
2293 kmod_module_versions_free_list(*list);
2294 *list = NULL;
2295 goto list_error;
2296 }
2297
2298 n = kmod_list_append(*list, mv);
2299 if (n != NULL)
2300 *list = n;
2301 else {
2302 kmod_module_version_free(mv);
2303 kmod_module_versions_free_list(*list);
2304 *list = NULL;
2305 ret = -ENOMEM;
2306 goto list_error;
2307 }
2308 }
2309 ret = count;
2310
2311list_error:
2312 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002313 return ret;
2314}
2315
2316/**
2317 * kmod_module_versions_get_symbol:
2318 * @entry: a list entry representing a kmod module versions
2319 *
2320 * Get the symbol of a kmod module versions.
2321 *
2322 * Returns: the symbol of this kmod module versions on success or NULL
2323 * on failure. The string is owned by the versions, do not free it.
2324 */
2325KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2326{
2327 struct kmod_module_version *version;
2328
2329 if (entry == NULL)
2330 return NULL;
2331
2332 version = entry->data;
2333 return version->symbol;
2334}
2335
2336/**
2337 * kmod_module_version_get_crc:
2338 * @entry: a list entry representing a kmod module version
2339 *
2340 * Get the crc of a kmod module version.
2341 *
2342 * Returns: the crc of this kmod module version on success or NULL on
2343 * failure. The string is owned by the version, do not free it.
2344 */
2345KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2346{
2347 struct kmod_module_version *version;
2348
2349 if (entry == NULL)
2350 return 0;
2351
2352 version = entry->data;
2353 return version->crc;
2354}
2355
2356/**
2357 * kmod_module_versions_free_list:
2358 * @list: kmod module versions list
2359 *
2360 * Release the resources taken by @list
2361 */
2362KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2363{
2364 while (list) {
2365 kmod_module_version_free(list->data);
2366 list = kmod_list_remove(list);
2367 }
2368}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002369
2370struct kmod_module_symbol {
2371 uint64_t crc;
2372 char symbol[];
2373};
2374
2375static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2376{
2377 struct kmod_module_symbol *mv;
2378 size_t symbollen = strlen(symbol) + 1;
2379
2380 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2381 if (mv == NULL)
2382 return NULL;
2383
2384 mv->crc = crc;
2385 memcpy(mv->symbol, symbol, symbollen);
2386 return mv;
2387}
2388
2389static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2390{
2391 free(symbol);
2392}
2393
2394/**
2395 * kmod_module_get_symbols:
2396 * @mod: kmod module
2397 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002398 * kmod_module_symbol_get_symbol() and
2399 * kmod_module_symbol_get_crc(). Release this list with
2400 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002401 *
2402 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2403 *
2404 * After use, free the @list by calling kmod_module_symbols_free_list().
2405 *
2406 * Returns: 0 on success or < 0 otherwise.
2407 */
2408KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2409{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002410 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002411 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002412 int i, count, ret = 0;
2413
2414 if (mod == NULL || list == NULL)
2415 return -ENOENT;
2416
2417 assert(*list == NULL);
2418
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002419 elf = kmod_module_get_elf(mod);
2420 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002421 return -errno;
2422
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002423 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002424 if (count < 0)
2425 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002426
2427 for (i = 0; i < count; i++) {
2428 struct kmod_module_symbol *mv;
2429 struct kmod_list *n;
2430
2431 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2432 if (mv == NULL) {
2433 ret = -errno;
2434 kmod_module_symbols_free_list(*list);
2435 *list = NULL;
2436 goto list_error;
2437 }
2438
2439 n = kmod_list_append(*list, mv);
2440 if (n != NULL)
2441 *list = n;
2442 else {
2443 kmod_module_symbol_free(mv);
2444 kmod_module_symbols_free_list(*list);
2445 *list = NULL;
2446 ret = -ENOMEM;
2447 goto list_error;
2448 }
2449 }
2450 ret = count;
2451
2452list_error:
2453 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002454 return ret;
2455}
2456
2457/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002458 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002459 * @entry: a list entry representing a kmod module symbols
2460 *
2461 * Get the symbol of a kmod module symbols.
2462 *
2463 * Returns: the symbol of this kmod module symbols on success or NULL
2464 * on failure. The string is owned by the symbols, do not free it.
2465 */
2466KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2467{
2468 struct kmod_module_symbol *symbol;
2469
2470 if (entry == NULL)
2471 return NULL;
2472
2473 symbol = entry->data;
2474 return symbol->symbol;
2475}
2476
2477/**
2478 * kmod_module_symbol_get_crc:
2479 * @entry: a list entry representing a kmod module symbol
2480 *
2481 * Get the crc of a kmod module symbol.
2482 *
2483 * Returns: the crc of this kmod module symbol on success or NULL on
2484 * failure. The string is owned by the symbol, do not free it.
2485 */
2486KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2487{
2488 struct kmod_module_symbol *symbol;
2489
2490 if (entry == NULL)
2491 return 0;
2492
2493 symbol = entry->data;
2494 return symbol->crc;
2495}
2496
2497/**
2498 * kmod_module_symbols_free_list:
2499 * @list: kmod module symbols list
2500 *
2501 * Release the resources taken by @list
2502 */
2503KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2504{
2505 while (list) {
2506 kmod_module_symbol_free(list->data);
2507 list = kmod_list_remove(list);
2508 }
2509}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002510
2511struct kmod_module_dependency_symbol {
2512 uint64_t crc;
2513 uint8_t bind;
2514 char symbol[];
2515};
2516
2517static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2518{
2519 struct kmod_module_dependency_symbol *mv;
2520 size_t symbollen = strlen(symbol) + 1;
2521
2522 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2523 if (mv == NULL)
2524 return NULL;
2525
2526 mv->crc = crc;
2527 mv->bind = bind;
2528 memcpy(mv->symbol, symbol, symbollen);
2529 return mv;
2530}
2531
2532static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2533{
2534 free(dependency_symbol);
2535}
2536
2537/**
2538 * kmod_module_get_dependency_symbols:
2539 * @mod: kmod module
2540 * @list: where to return list of module dependency_symbols. Use
2541 * kmod_module_dependency_symbol_get_symbol() and
2542 * kmod_module_dependency_symbol_get_crc(). Release this list with
2543 * kmod_module_dependency_symbols_free_list()
2544 *
2545 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2546 *
2547 * After use, free the @list by calling
2548 * kmod_module_dependency_symbols_free_list().
2549 *
2550 * Returns: 0 on success or < 0 otherwise.
2551 */
2552KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2553{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002554 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002555 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002556 int i, count, ret = 0;
2557
2558 if (mod == NULL || list == NULL)
2559 return -ENOENT;
2560
2561 assert(*list == NULL);
2562
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002563 elf = kmod_module_get_elf(mod);
2564 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002565 return -errno;
2566
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002567 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002568 if (count < 0)
2569 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002570
2571 for (i = 0; i < count; i++) {
2572 struct kmod_module_dependency_symbol *mv;
2573 struct kmod_list *n;
2574
2575 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2576 symbols[i].bind,
2577 symbols[i].symbol);
2578 if (mv == NULL) {
2579 ret = -errno;
2580 kmod_module_dependency_symbols_free_list(*list);
2581 *list = NULL;
2582 goto list_error;
2583 }
2584
2585 n = kmod_list_append(*list, mv);
2586 if (n != NULL)
2587 *list = n;
2588 else {
2589 kmod_module_dependency_symbol_free(mv);
2590 kmod_module_dependency_symbols_free_list(*list);
2591 *list = NULL;
2592 ret = -ENOMEM;
2593 goto list_error;
2594 }
2595 }
2596 ret = count;
2597
2598list_error:
2599 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002600 return ret;
2601}
2602
2603/**
2604 * kmod_module_dependency_symbol_get_symbol:
2605 * @entry: a list entry representing a kmod module dependency_symbols
2606 *
2607 * Get the dependency symbol of a kmod module
2608 *
2609 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2610 * on failure. The string is owned by the dependency_symbols, do not free it.
2611 */
2612KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2613{
2614 struct kmod_module_dependency_symbol *dependency_symbol;
2615
2616 if (entry == NULL)
2617 return NULL;
2618
2619 dependency_symbol = entry->data;
2620 return dependency_symbol->symbol;
2621}
2622
2623/**
2624 * kmod_module_dependency_symbol_get_crc:
2625 * @entry: a list entry representing a kmod module dependency_symbol
2626 *
2627 * Get the crc of a kmod module dependency_symbol.
2628 *
2629 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2630 * failure. The string is owned by the dependency_symbol, do not free it.
2631 */
2632KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2633{
2634 struct kmod_module_dependency_symbol *dependency_symbol;
2635
2636 if (entry == NULL)
2637 return 0;
2638
2639 dependency_symbol = entry->data;
2640 return dependency_symbol->crc;
2641}
2642
2643/**
2644 * kmod_module_dependency_symbol_get_bind:
2645 * @entry: a list entry representing a kmod module dependency_symbol
2646 *
2647 * Get the bind type of a kmod module dependency_symbol.
2648 *
2649 * Returns: the bind of this kmod module dependency_symbol on success
2650 * or < 0 on failure.
2651 */
2652KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2653{
2654 struct kmod_module_dependency_symbol *dependency_symbol;
2655
2656 if (entry == NULL)
2657 return 0;
2658
2659 dependency_symbol = entry->data;
2660 return dependency_symbol->bind;
2661}
2662
2663/**
2664 * kmod_module_dependency_symbols_free_list:
2665 * @list: kmod module dependency_symbols list
2666 *
2667 * Release the resources taken by @list
2668 */
2669KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2670{
2671 while (list) {
2672 kmod_module_dependency_symbol_free(list->data);
2673 list = kmod_list_remove(list);
2674 }
2675}