blob: f5384a7b2087508851516c67f48ba8a31a13bbe9 [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchia66a6a92012-01-09 00:40:50 -02004 * Copyright (C) 2011-2012 ProFUSION embedded systems
Lucas De Marchi8f788d52011-11-25 01:22:56 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi8f788d52011-11-25 01:22:56 -020010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Lucas De Marchi7636e722011-12-01 17:56:03 -020021#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020022#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020031#include <limits.h>
32#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/mman.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020036#include <sys/wait.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020037#include <string.h>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020038#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020039
40#include "libkmod.h"
41#include "libkmod-private.h"
42
43/**
Lucas De Marchi66819512012-01-09 04:47:40 -020044 * SECTION:libkmod-module
45 * @short_description: operate on kernel modules
46 */
47
48/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020049 * kmod_module:
50 *
51 * Opaque object representing a module.
52 */
53struct kmod_module {
54 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020055 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020056 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020057 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020058 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020059 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020060 const char *install_commands; /* owned by kmod_config */
61 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020062 char *alias; /* only set if this module was created from an alias */
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020063 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020064 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020065 struct {
66 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020067 bool options : 1;
68 bool install_commands : 1;
69 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020070 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020071
72 /*
73 * private field used by kmod_module_get_probe_list() to detect
74 * dependency loops
75 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020076 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020077
78 /*
79 * set by kmod_module_get_probe_list: indicates for probe_insert()
80 * whether the module's command and softdep should be ignored
81 */
82 bool ignorecmd : 1;
Lucas De Marchi38052742012-02-16 20:43:16 -020083
84 /*
85 * if module was created by searching the modules.builtin file, this
86 * is set. There's nothing much useful one can do with such a
87 * "module", except knowing it's builtin.
88 */
89 bool builtin : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020090};
91
Lucas De Marchic35347f2011-12-12 10:48:02 -020092static inline const char *path_join(const char *path, size_t prefixlen,
93 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020094{
95 size_t pathlen;
96
97 if (path[0] == '/')
98 return path;
99
100 pathlen = strlen(path);
101 if (prefixlen + pathlen + 1 >= PATH_MAX)
102 return NULL;
103
104 memcpy(buf + prefixlen, path, pathlen + 1);
105 return buf;
106}
107
Dave Reisneraf9572c2012-02-02 11:07:33 -0500108static inline bool module_is_inkernel(struct kmod_module *mod)
109{
110 int state = kmod_module_get_initstate(mod);
Lucas De Marchi38052742012-02-16 20:43:16 -0200111
Dave Reisneraf9572c2012-02-02 11:07:33 -0500112 if (state == KMOD_MODULE_LIVE ||
Dave Reisneraf9572c2012-02-02 11:07:33 -0500113 state == KMOD_MODULE_BUILTIN)
114 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200115
116 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500117}
118
Lucas De Marchi671d4892011-12-05 20:23:05 -0200119int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200120{
121 struct kmod_ctx *ctx = mod->ctx;
122 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200123 const char *dirname;
124 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200125 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200126 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200127 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200128
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200129 if (mod->init.dep)
130 return mod->n_dep;
131 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200132 mod->init.dep = true;
133
134 p = strchr(line, ':');
135 if (p == NULL)
136 return 0;
137
Lucas De Marchi671d4892011-12-05 20:23:05 -0200138 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200139 dirname = kmod_get_dirname(mod->ctx);
140 dirnamelen = strlen(dirname);
141 if (dirnamelen + 2 >= PATH_MAX)
142 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200143
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200144 memcpy(buf, dirname, dirnamelen);
145 buf[dirnamelen] = '/';
146 dirnamelen++;
147 buf[dirnamelen] = '\0';
148
149 if (mod->path == NULL) {
150 const char *str = path_join(line, dirnamelen, buf);
151 if (str == NULL)
152 return 0;
153 mod->path = strdup(str);
154 if (mod->path == NULL)
155 return 0;
156 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200157
Lucas De Marchi7636e722011-12-01 17:56:03 -0200158 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200159 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
160 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200161 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200162 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200163
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200164 path = path_join(p, dirnamelen, buf);
165 if (path == NULL) {
166 ERR(ctx, "could not join path '%s' and '%s'.\n",
167 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200168 goto fail;
169 }
170
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200171 err = kmod_module_new_from_path(ctx, path, &depmod);
172 if (err < 0) {
173 ERR(ctx, "ctx=%p path=%s error=%s\n",
174 ctx, path, strerror(-err));
175 goto fail;
176 }
177
178 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200179
Lucas De Marchib94a7372011-12-26 20:10:49 -0200180 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200181 n++;
182 }
183
184 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
185
186 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200187 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200188 return n;
189
190fail:
191 kmod_module_unref_list(list);
192 mod->init.dep = false;
193 return err;
194}
195
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200196void kmod_module_set_visited(struct kmod_module *mod, bool visited)
197{
198 mod->visited = visited;
199}
200
Lucas De Marchi38052742012-02-16 20:43:16 -0200201void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
202{
203 mod->builtin = builtin;
204}
205
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200206/*
207 * Memory layout with alias:
208 *
209 * struct kmod_module {
210 * hashkey -----.
211 * alias -----. |
212 * name ----. | |
213 * } | | |
214 * name <----------' | |
215 * alias <-----------' |
216 * name\alias <--------'
217 *
218 * Memory layout without alias:
219 *
220 * struct kmod_module {
221 * hashkey ---.
222 * alias -----|----> NULL
223 * name ----. |
224 * } | |
225 * name <----------'-'
226 *
227 * @key is "name\alias" or "name" (in which case alias == NULL)
228 */
229static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
230 const char *name, size_t namelen,
231 const char *alias, size_t aliaslen,
232 struct kmod_module **mod)
233{
234 struct kmod_module *m;
235 size_t keylen;
236
237 m = kmod_pool_get_module(ctx, key);
238 if (m != NULL) {
239 *mod = kmod_module_ref(m);
240 return 0;
241 }
242
243 if (alias == NULL)
244 keylen = namelen;
245 else
246 keylen = namelen + aliaslen + 1;
247
248 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
249 if (m == NULL) {
250 free(m);
251 return -ENOMEM;
252 }
253
254 memset(m, 0, sizeof(*m));
255
256 m->ctx = kmod_ref(ctx);
257 m->name = (char *)m + sizeof(*m);
258 memcpy(m->name, key, keylen + 1);
259 if (alias == NULL) {
260 m->hashkey = m->name;
261 m->alias = NULL;
262 } else {
263 m->name[namelen] = '\0';
264 m->alias = m->name + namelen + 1;
265 m->hashkey = m->name + keylen + 1;
266 memcpy(m->hashkey, key, keylen + 1);
267 }
268
269 m->refcount = 1;
270 kmod_pool_add_module(ctx, m, m->hashkey);
271 *mod = m;
272
273 return 0;
274}
275
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200276/**
277 * kmod_module_new_from_name:
278 * @ctx: kmod library context
279 * @name: name of the module
280 * @mod: where to save the created struct kmod_module
281 *
282 * Create a new struct kmod_module using the module name. @name can not be an
283 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600284 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200285 *
286 * This function is also used internally by many others that return a new
287 * struct kmod_module or a new list of modules.
288 *
289 * The initial refcount is 1, and needs to be decremented to release the
290 * resources of the kmod_module. Since libkmod keeps track of all
291 * kmod_modules created, they are all released upon @ctx destruction too. Do
292 * not unref @ctx before all the desired operations with the returned
293 * kmod_module are done.
294 *
295 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
296 * module name or if memory allocation failed.
297 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200298KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
299 const char *name,
300 struct kmod_module **mod)
301{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200302 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200303 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200304
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200305 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200306 return -ENOENT;
307
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200308 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200309
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200310 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200311}
312
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200313int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
314 const char *name, struct kmod_module **mod)
315{
316 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200317 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200318 size_t namelen = strlen(name);
319 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200320
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200321 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200322 return -ENAMETOOLONG;
323
324 memcpy(key, name, namelen);
325 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200326 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200327
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200328 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200329 if (err < 0)
330 return err;
331
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200332 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200333}
334
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200335/**
336 * kmod_module_new_from_path:
337 * @ctx: kmod library context
338 * @path: path where to find the given module
339 * @mod: where to save the created struct kmod_module
340 *
341 * Create a new struct kmod_module using the module path. @path must be an
342 * existent file with in the filesystem and must be accessible to libkmod.
343 *
344 * The initial refcount is 1, and needs to be decremented to release the
345 * resources of the kmod_module. Since libkmod keeps track of all
346 * kmod_modules created, they are all released upon @ctx destruction too. Do
347 * not unref @ctx before all the desired operations with the returned
348 * kmod_module are done.
349 *
350 * If @path is relative, it's treated as relative to the current working
351 * directory. Otherwise, give an absolute path.
352 *
353 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
354 * it's not a valid file for a kmod_module or if memory allocation failed.
355 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200356KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
357 const char *path,
358 struct kmod_module **mod)
359{
360 struct kmod_module *m;
361 int err;
362 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200363 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200364 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200365 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200366
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200367 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200368 return -ENOENT;
369
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200370 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200371 if (abspath == NULL) {
372 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200373 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200374 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200375
376 err = stat(abspath, &st);
377 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200378 err = -errno;
379 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200380 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200381 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200382 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200383
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200384 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200385 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200386 free(abspath);
387 return -ENOENT;
388 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200389
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200390 m = kmod_pool_get_module(ctx, name);
391 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200392 if (m->path == NULL)
393 m->path = abspath;
394 else if (streq(m->path, abspath))
395 free(abspath);
396 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200397 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 -0200398 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200399 free(abspath);
400 return -EEXIST;
401 }
402
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200403 *mod = kmod_module_ref(m);
404 return 0;
405 }
406
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200407 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
408 if (err < 0)
409 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200410
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200411 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200412 *mod = m;
413
414 return 0;
415}
416
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200417/**
418 * kmod_module_unref:
419 * @mod: kmod module
420 *
421 * Drop a reference of the kmod module. If the refcount reaches zero, its
422 * resources are released.
423 *
424 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
425 * returns the passed @mod with its refcount decremented.
426 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200427KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
428{
429 if (mod == NULL)
430 return NULL;
431
432 if (--mod->refcount > 0)
433 return mod;
434
435 DBG(mod->ctx, "kmod_module %p released\n", mod);
436
Lucas De Marchi4084c172011-12-15 13:43:22 -0200437 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200438 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200439 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200440 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200441 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200442 free(mod);
443 return NULL;
444}
445
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200446/**
447 * kmod_module_ref:
448 * @mod: kmod module
449 *
450 * Take a reference of the kmod module, incrementing its refcount.
451 *
452 * Returns: the passed @module with its refcount incremented.
453 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200454KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
455{
456 if (mod == NULL)
457 return NULL;
458
459 mod->refcount++;
460
461 return mod;
462}
463
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200464#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
465 do { \
466 if ((_err) < 0) \
467 goto _label_err; \
468 if (*(_list) != NULL) \
469 goto finish; \
470 } while (0)
471
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200472/**
473 * kmod_module_new_from_lookup:
474 * @ctx: kmod library context
475 * @given_alias: alias to look for
476 * @list: an empty list where to save the list of modules matching
477 * @given_alias
478 *
479 * Create a new list of kmod modules using an alias or module name and lookup
480 * libkmod's configuration files and indexes in order to find the module.
481 * Once it's found in one of the places, it stops searching and create the
482 * list of modules that is saved in @list.
483 *
484 * The search order is: 1. aliases in configuration file; 2. module names in
485 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
486 * in modules.alias index.
487 *
488 * The initial refcount is 1, and needs to be decremented to release the
489 * resources of the kmod_module. The returned @list must be released by
490 * calling kmod_module_unref_list(). Since libkmod keeps track of all
491 * kmod_modules created, they are all released upon @ctx destruction too. Do
492 * not unref @ctx before all the desired operations with the returned list are
493 * completed.
494 *
495 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
496 * methods failed, which is basically due to memory allocation fail. If module
497 * is not found, it still returns 0, but @list is an empty list.
498 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200499KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200500 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200501 struct kmod_list **list)
502{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200503 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200504 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200505
Lucas De Marchi4308b172011-12-13 10:26:04 -0200506 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200507 return -ENOENT;
508
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200509 if (list == NULL || *list != NULL) {
510 ERR(ctx, "An empty list is needed to create lookup\n");
511 return -ENOSYS;
512 }
513
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200514 if (alias_normalize(given_alias, alias, NULL) < 0) {
515 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200516 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200517 }
518
519 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200520
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200521 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200522 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200523 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200524
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200525 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200526 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
527 CHECK_ERR_AND_FINISH(err, fail, list, finish);
528
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200529 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200530 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
531 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200532
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200533 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200534 err = kmod_lookup_alias_from_commands(ctx, alias, list);
535 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200536
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200537 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200538 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
539 CHECK_ERR_AND_FINISH(err, fail, list, finish);
540
Lucas De Marchi38052742012-02-16 20:43:16 -0200541 DBG(ctx, "lookup modules.builtin %s\n", alias);
542 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
543 CHECK_ERR_AND_FINISH(err, fail, list, finish);
544
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200545finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200546 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200547 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200548fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200549 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200550 kmod_module_unref_list(*list);
551 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200552 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200553}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200554#undef CHECK_ERR_AND_FINISH
555
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200556/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200557 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200558 * @list: list of kmod modules
559 *
560 * Drop a reference of each kmod module in @list and releases the resources
561 * taken by the list itself.
562 *
563 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
564 * returns the passed @mod with its refcount decremented.
565 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200566KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
567{
568 for (; list != NULL; list = kmod_list_remove(list))
569 kmod_module_unref(list->data);
570
571 return 0;
572}
573
Lucas De Marchi0d467432011-12-31 11:15:52 -0200574/**
575 * kmod_module_get_filtered_blacklist:
576 * @ctx: kmod library context
577 * @input: list of kmod_module to be filtered with blacklist
578 * @output: where to save the new list
579 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200580 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500581 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200582 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500583 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200584 *
585 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
586 * list.
587 */
588KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
589 const struct kmod_list *input,
590 struct kmod_list **output)
591{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500592 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200593}
594
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200595static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
596{
597 if (!mod->init.dep) {
598 /* lazy init */
599 char *line = kmod_search_moddep(mod->ctx, mod->name);
600
601 if (line == NULL)
602 return NULL;
603
604 kmod_module_parse_depline((struct kmod_module *)mod, line);
605 free(line);
606
607 if (!mod->init.dep)
608 return NULL;
609 }
610
611 return mod->dep;
612}
613
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200614/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200615 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200616 * @mod: kmod module
617 *
618 * Search the modules.dep index to find the dependencies of the given @mod.
619 * The result is cached in @mod, so subsequent calls to this function will
620 * return the already searched list of modules.
621 *
622 * Returns: NULL on failure or if there are any dependencies. Otherwise it
623 * returns a list of kmod modules that can be released by calling
624 * kmod_module_unref_list().
625 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200626KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200627{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200628 struct kmod_list *l, *l_new, *list_new = NULL;
629
630 if (mod == NULL)
631 return NULL;
632
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200633 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200634
635 kmod_list_foreach(l, mod->dep) {
636 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
637 if (l_new == NULL) {
638 kmod_module_unref(l->data);
639 goto fail;
640 }
641
642 list_new = l_new;
643 }
644
645 return list_new;
646
647fail:
648 ERR(mod->ctx, "out of memory\n");
649 kmod_module_unref_list(list_new);
650 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200651}
652
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200653/**
654 * kmod_module_get_module:
655 * @entry: an entry in a list of kmod modules.
656 *
657 * Get the kmod module of this @entry in the list, increasing its refcount.
658 * After it's used, unref it. Since the refcount is incremented upon return,
659 * you still have to call kmod_module_unref_list() to release the list of kmod
660 * modules.
661 *
662 * Returns: NULL on failure or the kmod_module contained in this list entry
663 * with its refcount incremented.
664 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200665KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200666{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200667 if (entry == NULL)
668 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200669
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200670 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200671}
672
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200673/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200674 * kmod_module_get_name:
675 * @mod: kmod module
676 *
677 * Get the name of this kmod module. Name is always available, independently
678 * if it was created by kmod_module_new_from_name() or another function and
679 * it's always normalized (dashes are replaced with underscores).
680 *
681 * Returns: the name of this kmod module.
682 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200683KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200684{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200685 if (mod == NULL)
686 return NULL;
687
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200688 return mod->name;
689}
690
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200691/**
692 * kmod_module_get_path:
693 * @mod: kmod module
694 *
695 * Get the path of this kmod module. If this kmod module was not created by
696 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200697 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200698 *
699 * Returns: the path of this kmod module or NULL if such information is not
700 * available.
701 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200702KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200703{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200704 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200705
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200706 if (mod == NULL)
707 return NULL;
708
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200709 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200710
Lucas De Marchie005fac2011-12-08 10:42:34 -0200711 if (mod->path != NULL)
712 return mod->path;
713 if (mod->init.dep)
714 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200715
Lucas De Marchie005fac2011-12-08 10:42:34 -0200716 /* lazy init */
717 line = kmod_search_moddep(mod->ctx, mod->name);
718 if (line == NULL)
719 return NULL;
720
721 kmod_module_parse_depline((struct kmod_module *) mod, line);
722 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200723
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200724 return mod->path;
725}
726
727
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200728extern long delete_module(const char *name, unsigned int flags);
729
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200730/**
731 * kmod_module_remove_module:
732 * @mod: kmod module
733 * @flags: flags to pass to Linux kernel when removing the module
734 *
735 * Remove a module from Linux kernel.
736 *
737 * Returns: 0 on success or < 0 on failure.
738 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200739KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
740 unsigned int flags)
741{
742 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200743
744 if (mod == NULL)
745 return -ENOENT;
746
747 /* Filter out other flags */
748 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
749
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200750 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200751 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200752 err = -errno;
753 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200754 }
755
Lucas De Marchiba998b92012-01-11 00:08:14 -0200756 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200757}
758
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200759extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200760
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200761/**
762 * kmod_module_insert_module:
763 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200764 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
765 * behavior of this function.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200766 * @options: module's options to pass to Linux Kernel.
767 *
768 * Insert a module in Linux kernel. It opens the file pointed by @mod,
769 * mmap'ing it and passing to kernel.
770 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200771 * Returns: 0 on success or < 0 on failure. If module is already loaded it
772 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200773 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200774KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200775 unsigned int flags,
776 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200777{
778 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200779 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200780 off_t size;
781 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200782 struct kmod_elf *elf = NULL;
783 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200784 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200785
786 if (mod == NULL)
787 return -ENOENT;
788
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200789 path = kmod_module_get_path(mod);
790 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500791 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200792 return -ENOSYS;
793 }
794
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200795 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200796 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200797 err = -errno;
798 return err;
799 }
800
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200801 size = kmod_file_get_size(file);
802 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200803
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200804 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
805 elf = kmod_elf_new(mem, size);
806 if (elf == NULL) {
807 err = -errno;
808 goto elf_failed;
809 }
810
811 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
812 err = kmod_elf_strip_section(elf, "__versions");
813 if (err < 0)
814 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
815 }
816
817 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
818 err = kmod_elf_strip_vermagic(elf);
819 if (err < 0)
820 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
821 }
822
823 mem = kmod_elf_get_memory(elf);
824 }
825
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200826 err = init_module(mem, size, args);
Lucas De Marchibbf59322011-12-30 14:13:33 -0200827 if (err < 0) {
828 err = -errno;
829 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
830 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200831
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200832 if (elf != NULL)
833 kmod_elf_unref(elf);
834elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200835 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200836
837 return err;
838}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200839
Lucas De Marchiddbda022011-12-27 11:40:10 -0200840static bool module_is_blacklisted(struct kmod_module *mod)
841{
842 struct kmod_ctx *ctx = mod->ctx;
843 const struct kmod_list *bl = kmod_get_blacklists(ctx);
844 const struct kmod_list *l;
845
846 kmod_list_foreach(l, bl) {
847 const char *modname = kmod_blacklist_get_modname(l);
848
849 if (streq(modname, mod->name))
850 return true;
851 }
852
853 return false;
854}
855
Dave Reisnerd80b1032012-02-24 10:05:11 -0500856/**
857 * kmod_module_apply_filter
858 * @ctx: kmod library context
859 * @filter_type: bitmask to filter modules on
860 * @input: list of kmod_module to be filtered
861 * @output: where to save the new list
862 *
863 * Given a list @input, this function filter it out by the filter mask
864 * and save it in @output.
865 *
866 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
867 * list.
868 */
869KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
870 enum kmod_filter filter_type,
871 const struct kmod_list *input,
872 struct kmod_list **output)
873{
874 const struct kmod_list *li;
875
876 if (ctx == NULL || output == NULL)
877 return -ENOENT;
878
879 *output = NULL;
880 if (input == NULL)
881 return 0;
882
883 kmod_list_foreach(li, input) {
884 struct kmod_module *mod = li->data;
885 struct kmod_list *node;
886
887 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
888 module_is_blacklisted(mod))
889 continue;
890
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500891 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500892 continue;
893
894 node = kmod_list_append(*output, mod);
895 if (node == NULL)
896 goto fail;
897
898 *output = node;
899 kmod_module_ref(mod);
900 }
901
902 return 0;
903
904fail:
905 kmod_module_unref_list(*output);
906 *output = NULL;
907 return -ENOMEM;
908}
909
Lucas De Marchiddbda022011-12-27 11:40:10 -0200910static int command_do(struct kmod_module *mod, const char *type,
911 const char *cmd)
912{
913 const char *modname = kmod_module_get_name(mod);
914 int err;
915
916 DBG(mod->ctx, "%s %s\n", type, cmd);
917
918 setenv("MODPROBE_MODULE", modname, 1);
919 err = system(cmd);
920 unsetenv("MODPROBE_MODULE");
921
922 if (err == -1 || WEXITSTATUS(err)) {
923 ERR(mod->ctx, "Error running %s command for %s\n",
924 type, modname);
925 if (err != -1)
926 err = -WEXITSTATUS(err);
927 }
928
929 return err;
930}
931
Lucas De Marchib1a51252012-01-29 01:49:09 -0200932struct probe_insert_cb {
933 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
934 void *data;
935};
936
Lucas De Marchiddbda022011-12-27 11:40:10 -0200937static int module_do_install_commands(struct kmod_module *mod,
938 const char *options,
939 struct probe_insert_cb *cb)
940{
941 const char *command = kmod_module_get_install_commands(mod);
942 char *p, *cmd;
943 int err;
944 size_t cmdlen, options_len, varlen;
945
946 assert(command);
947
948 if (options == NULL)
949 options = "";
950
951 options_len = strlen(options);
952 cmdlen = strlen(command);
953 varlen = sizeof("$CMDLINE_OPTS") - 1;
954
955 cmd = memdup(command, cmdlen + 1);
956 if (cmd == NULL)
957 return -ENOMEM;
958
959 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
960 size_t prefixlen = p - cmd;
961 size_t suffixlen = cmdlen - prefixlen - varlen;
962 size_t slen = cmdlen - varlen + options_len;
963 char *suffix = p + varlen;
964 char *s = malloc(slen + 1);
965 if (s == NULL) {
966 free(cmd);
967 return -ENOMEM;
968 }
969 memcpy(s, cmd, p - cmd);
970 memcpy(s + prefixlen, options, options_len);
971 memcpy(s + prefixlen + options_len, suffix, suffixlen);
972 s[slen] = '\0';
973
974 free(cmd);
975 cmd = s;
976 cmdlen = slen;
977 }
978
979 if (cb->run_install != NULL)
980 err = cb->run_install(mod, cmd, cb->data);
981 else
982 err = command_do(mod, "install", cmd);
983
984 free(cmd);
985
986 return err;
987}
988
Lucas De Marchiddbda022011-12-27 11:40:10 -0200989static char *module_options_concat(const char *opt, const char *xopt)
990{
991 // TODO: we might need to check if xopt overrides options on opt
992 size_t optlen = opt == NULL ? 0 : strlen(opt);
993 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
994 char *r;
995
996 if (optlen == 0 && xoptlen == 0)
997 return NULL;
998
999 r = malloc(optlen + xoptlen + 2);
1000
1001 if (opt != NULL) {
1002 memcpy(r, opt, optlen);
1003 r[optlen] = ' ';
1004 optlen++;
1005 }
1006
1007 if (xopt != NULL)
1008 memcpy(r + optlen, xopt, xoptlen);
1009
1010 r[optlen + xoptlen] = '\0';
1011
1012 return r;
1013}
1014
Lucas De Marchib1a51252012-01-29 01:49:09 -02001015static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001016 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001017 struct kmod_list **list);
1018
1019/* re-entrant */
1020static int __kmod_module_fill_softdep(struct kmod_module *mod,
1021 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001022{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001023 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001024 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001025
1026 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001027 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001028 ERR(mod->ctx, "could not get softdep: %s\n",
1029 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001030 goto fail;
1031 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001032
Lucas De Marchib1a51252012-01-29 01:49:09 -02001033 kmod_list_foreach(l, pre) {
1034 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001035 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001036 if (err < 0)
1037 goto fail;
1038 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001039
Lucas De Marchib1a51252012-01-29 01:49:09 -02001040 l = kmod_list_append(*list, kmod_module_ref(mod));
1041 if (l == NULL) {
1042 kmod_module_unref(mod);
1043 err = -ENOMEM;
1044 goto fail;
1045 }
1046 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001047 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001048
Lucas De Marchib1a51252012-01-29 01:49:09 -02001049 kmod_list_foreach(l, post) {
1050 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001051 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001052 if (err < 0)
1053 goto fail;
1054 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001055
Lucas De Marchib1a51252012-01-29 01:49:09 -02001056fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001057 kmod_module_unref_list(pre);
1058 kmod_module_unref_list(post);
1059
1060 return err;
1061}
1062
Lucas De Marchib1a51252012-01-29 01:49:09 -02001063/* re-entrant */
1064static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001065 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001066 struct kmod_list **list)
1067{
1068 struct kmod_list *dep, *l;
1069 int err = 0;
1070
1071 if (mod->visited) {
1072 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1073 mod->name);
1074 return 0;
1075 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001076 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001077
1078 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001079 kmod_list_foreach(l, dep) {
1080 struct kmod_module *m = l->data;
1081 err = __kmod_module_fill_softdep(m, list);
1082 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001083 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001084 }
1085
Lucas De Marchi89e92482012-01-29 02:35:46 -02001086 if (ignorecmd) {
1087 l = kmod_list_append(*list, kmod_module_ref(mod));
1088 if (l == NULL) {
1089 kmod_module_unref(mod);
1090 err = -ENOMEM;
1091 goto finish;
1092 }
1093 *list = l;
1094 mod->ignorecmd = true;
1095 } else
1096 err = __kmod_module_fill_softdep(mod, list);
1097
Lucas De Marchib1a51252012-01-29 01:49:09 -02001098finish:
1099 kmod_module_unref_list(dep);
1100 return err;
1101}
1102
1103static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001104 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001105 struct kmod_list **list)
1106{
1107 int err;
1108
1109 assert(mod != NULL);
1110 assert(list != NULL && *list == NULL);
1111
1112 /*
1113 * Make sure we don't get screwed by previous calls to this function
1114 */
1115 kmod_set_modules_visited(mod->ctx, false);
1116
Lucas De Marchi89e92482012-01-29 02:35:46 -02001117 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001118 if (err < 0) {
1119 kmod_module_unref_list(*list);
1120 *list = NULL;
1121 }
1122
1123 return err;
1124}
1125
Lucas De Marchiddbda022011-12-27 11:40:10 -02001126/**
1127 * kmod_module_probe_insert_module:
1128 * @mod: kmod module
1129 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1130 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001131 * @extra_options: module's options to pass to Linux Kernel. It applies only
1132 * to @mod, not to its dependencies.
1133 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001134 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001135 * @print_action: function to call with the action being taken (install or
1136 * insmod). It's useful for tools like modprobe when running with verbose
1137 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001138 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001139 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001140 * install commands and applying blacklist.
1141 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001142 * If @run_install is NULL, this function will fork and exec by calling
1143 * system(3). Don't pass a NULL argument in @run_install if your binary is
1144 * setuid/setgid (see warning in system(3)). If you need control over the
1145 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001146 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001147 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1148 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001149 */
1150KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1151 unsigned int flags, const char *extra_options,
1152 int (*run_install)(struct kmod_module *m,
1153 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001154 const void *data,
1155 void (*print_action)(struct kmod_module *m,
1156 bool install,
1157 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001158{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001159 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001160 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001161 int err;
1162
1163 if (mod == NULL)
1164 return -ENOENT;
1165
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001166 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1167 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001168 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001169 return -EEXIST;
1170 else
1171 return 0;
1172 }
1173
Lucas De Marchib1a51252012-01-29 01:49:09 -02001174 err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
1175 KMOD_PROBE_APPLY_BLACKLIST_ALL);
1176 if (err != 0) {
1177 if (module_is_blacklisted(mod))
1178 return err;
1179 }
1180
Lucas De Marchi89e92482012-01-29 02:35:46 -02001181 err = kmod_module_get_probe_list(mod,
1182 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001183 if (err < 0)
1184 return err;
1185
1186 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1187 struct kmod_list *filtered = NULL;
1188
Dave Reisnerd80b1032012-02-24 10:05:11 -05001189 err = kmod_module_apply_filter(mod->ctx,
1190 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001191 if (err < 0)
1192 return err;
1193
1194 kmod_module_unref_list(list);
1195 if (filtered == NULL)
1196 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1197
1198 list = filtered;
1199 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001200
1201 cb.run_install = run_install;
1202 cb.data = (void *) data;
1203
Lucas De Marchib1a51252012-01-29 01:49:09 -02001204 kmod_list_foreach(l, list) {
1205 struct kmod_module *m = l->data;
1206 const char *moptions = kmod_module_get_options(m);
1207 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001208 char *options;
1209
1210 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1211 && module_is_inkernel(m)) {
1212 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1213 m->name);
1214 err = -EEXIST;
1215 goto finish_module;
1216 }
1217
1218 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001219 m == mod ? extra_options : NULL);
1220
Lucas De Marchi89e92482012-01-29 02:35:46 -02001221 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001222 if (print_action != NULL)
1223 print_action(m, true, options ?: "");
1224
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001225 if (!(flags & KMOD_PROBE_DRY_RUN))
1226 err = module_do_install_commands(m, options,
1227 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001228 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001229 if (print_action != NULL)
1230 print_action(m, false, options ?: "");
1231
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001232 if (!(flags & KMOD_PROBE_DRY_RUN))
1233 err = kmod_module_insert_module(m, flags,
1234 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001235 }
1236
1237 free(options);
1238
Lucas De Marchiabd55572012-02-19 04:20:30 -02001239finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001240 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001241 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001242 * already loaded and the module being loaded is not a softdep
1243 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001244 *
1245 * We need to check here because of race conditions. We
1246 * checked first if module was already loaded but it may have
1247 * been loaded between the check and the moment we try to
1248 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001249 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001250 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001251 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001252 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001253
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001254 if (err == -EEXIST)
1255 err = 0;
1256 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001257 break;
1258 }
1259
1260 kmod_module_unref_list(list);
1261 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001262}
1263
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001264/**
1265 * kmod_module_get_options:
1266 * @mod: kmod module
1267 *
1268 * Get options of this kmod module. Options come from the configuration file
1269 * and are cached in @mod. The first call to this function will search for
1270 * this module in configuration and subsequent calls return the cached string.
1271 *
1272 * Returns: a string with all the options separated by spaces. This string is
1273 * owned by @mod, do not free it.
1274 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001275KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1276{
1277 if (mod == NULL)
1278 return NULL;
1279
1280 if (!mod->init.options) {
1281 /* lazy init */
1282 struct kmod_module *m = (struct kmod_module *)mod;
1283 const struct kmod_list *l, *ctx_options;
1284 char *opts = NULL;
1285 size_t optslen = 0;
1286
1287 ctx_options = kmod_get_options(mod->ctx);
1288
1289 kmod_list_foreach(l, ctx_options) {
1290 const char *modname = kmod_option_get_modname(l);
1291 const char *str;
1292 size_t len;
1293 void *tmp;
1294
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001295 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1296 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1297 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001298 continue;
1299
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001300 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 -02001301 str = kmod_option_get_options(l);
1302 len = strlen(str);
1303 if (len < 1)
1304 continue;
1305
1306 tmp = realloc(opts, optslen + len + 2);
1307 if (tmp == NULL) {
1308 free(opts);
1309 goto failed;
1310 }
1311
1312 opts = tmp;
1313
1314 if (optslen > 0) {
1315 opts[optslen] = ' ';
1316 optslen++;
1317 }
1318
1319 memcpy(opts + optslen, str, len);
1320 optslen += len;
1321 opts[optslen] = '\0';
1322 }
1323
1324 m->init.options = true;
1325 m->options = opts;
1326 }
1327
1328 return mod->options;
1329
1330failed:
1331 ERR(mod->ctx, "out of memory\n");
1332 return NULL;
1333}
1334
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001335/**
1336 * kmod_module_get_install_commands:
1337 * @mod: kmod module
1338 *
1339 * Get install commands for this kmod module. Install commands come from the
1340 * configuration file and are cached in @mod. The first call to this function
1341 * will search for this module in configuration and subsequent calls return
1342 * the cached string. The install commands are returned as they were in the
1343 * configuration, concatenated by ';'. No other processing is made in this
1344 * string.
1345 *
1346 * Returns: a string with all install commands separated by semicolons. This
1347 * string is owned by @mod, do not free it.
1348 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001349KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1350{
1351 if (mod == NULL)
1352 return NULL;
1353
1354 if (!mod->init.install_commands) {
1355 /* lazy init */
1356 struct kmod_module *m = (struct kmod_module *)mod;
1357 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001358
1359 ctx_install_commands = kmod_get_install_commands(mod->ctx);
1360
1361 kmod_list_foreach(l, ctx_install_commands) {
1362 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001363
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001364 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001365 continue;
1366
Lucas De Marchi60f67602011-12-16 03:33:26 -02001367 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001368
Lucas De Marchi60f67602011-12-16 03:33:26 -02001369 /*
1370 * find only the first command, as modprobe from
1371 * module-init-tools does
1372 */
1373 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001374 }
1375
1376 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001377 }
1378
1379 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001380}
1381
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001382void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1383{
1384 mod->init.install_commands = true;
1385 mod->install_commands = cmd;
1386}
1387
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001388static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1389{
1390 struct kmod_list *ret = NULL;
1391 unsigned i;
1392
1393 for (i = 0; i < count; i++) {
1394 const char *depname = array[i];
1395 struct kmod_list *lst = NULL;
1396 int err;
1397
1398 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1399 if (err < 0) {
1400 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1401 continue;
1402 } else if (lst != NULL)
1403 ret = kmod_list_append_list(ret, lst);
1404 }
1405 return ret;
1406}
1407
1408/**
1409 * kmod_module_get_softdeps:
1410 * @mod: kmod module
1411 * @pre: where to save the list of preceding soft dependencies.
1412 * @post: where to save the list of post soft dependencies.
1413 *
1414 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001415 * from configuration file and are not cached in @mod because it may include
1416 * dependency cycles that would make we leak kmod_module. Any call
1417 * to this function will search for this module in configuration, allocate a
1418 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001419 *
1420 * Both @pre and @post are newly created list of kmod_module and
1421 * should be unreferenced with kmod_module_unref_list().
1422 *
1423 * Returns: 0 on success or < 0 otherwise.
1424 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001425KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1426 struct kmod_list **pre,
1427 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001428{
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001429 const struct kmod_list *l, *ctx_softdeps;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001430
1431 if (mod == NULL || pre == NULL || post == NULL)
1432 return -ENOENT;
1433
1434 assert(*pre == NULL);
1435 assert(*post == NULL);
1436
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001437 ctx_softdeps = kmod_get_softdeps(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001438
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001439 kmod_list_foreach(l, ctx_softdeps) {
1440 const char *modname = kmod_softdep_get_name(l);
1441 const char * const *array;
1442 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001443
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001444 if (fnmatch(modname, mod->name, 0) != 0)
1445 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001446
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001447 array = kmod_softdep_get_pre(l, &count);
1448 *pre = lookup_softdep(mod->ctx, array, count);
1449 array = kmod_softdep_get_post(l, &count);
1450 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001451
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001452 /*
1453 * find only the first command, as modprobe from
1454 * module-init-tools does
1455 */
1456 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001457 }
1458
1459 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001460}
1461
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001462/**
1463 * kmod_module_get_remove_commands:
1464 * @mod: kmod module
1465 *
1466 * Get remove commands for this kmod module. Remove commands come from the
1467 * configuration file and are cached in @mod. The first call to this function
1468 * will search for this module in configuration and subsequent calls return
1469 * the cached string. The remove commands are returned as they were in the
1470 * configuration, concatenated by ';'. No other processing is made in this
1471 * string.
1472 *
1473 * Returns: a string with all remove commands separated by semicolons. This
1474 * string is owned by @mod, do not free it.
1475 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001476KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1477{
1478 if (mod == NULL)
1479 return NULL;
1480
1481 if (!mod->init.remove_commands) {
1482 /* lazy init */
1483 struct kmod_module *m = (struct kmod_module *)mod;
1484 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001485
1486 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1487
1488 kmod_list_foreach(l, ctx_remove_commands) {
1489 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001490
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001491 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001492 continue;
1493
Lucas De Marchi60f67602011-12-16 03:33:26 -02001494 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001495
Lucas De Marchi60f67602011-12-16 03:33:26 -02001496 /*
1497 * find only the first command, as modprobe from
1498 * module-init-tools does
1499 */
1500 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001501 }
1502
1503 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001504 }
1505
1506 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001507}
1508
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001509void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1510{
1511 mod->init.remove_commands = true;
1512 mod->remove_commands = cmd;
1513}
1514
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001515/**
1516 * SECTION:libkmod-loaded
1517 * @short_description: currently loaded modules
1518 *
1519 * Information about currently loaded modules, as reported by Linux kernel.
1520 * These information are not cached by libkmod and are always read from /sys
1521 * and /proc/modules.
1522 */
1523
1524/**
1525 * kmod_module_new_from_loaded:
1526 * @ctx: kmod library context
1527 * @list: where to save the list of loaded modules
1528 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001529 * Create a new list of kmod modules with all modules currently loaded in
1530 * kernel. It uses /proc/modules to get the names of loaded modules and to
1531 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1532 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001533 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001534 * The initial refcount is 1, and needs to be decremented to release the
1535 * resources of the kmod_module. The returned @list must be released by
1536 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1537 * kmod_modules created, they are all released upon @ctx destruction too. Do
1538 * not unref @ctx before all the desired operations with the returned list are
1539 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001540 *
1541 * Returns: 0 on success or < 0 on error.
1542 */
1543KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1544 struct kmod_list **list)
1545{
1546 struct kmod_list *l = NULL;
1547 FILE *fp;
1548 char line[4096];
1549
1550 if (ctx == NULL || list == NULL)
1551 return -ENOENT;
1552
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001553 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001554 if (fp == NULL) {
1555 int err = -errno;
1556 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1557 return err;
1558 }
1559
1560 while (fgets(line, sizeof(line), fp)) {
1561 struct kmod_module *m;
1562 struct kmod_list *node;
1563 int err;
1564 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1565
1566 err = kmod_module_new_from_name(ctx, name, &m);
1567 if (err < 0) {
1568 ERR(ctx, "could not get module from name '%s': %s\n",
1569 name, strerror(-err));
1570 continue;
1571 }
1572
1573 node = kmod_list_append(l, m);
1574 if (node)
1575 l = node;
1576 else {
1577 ERR(ctx, "out of memory\n");
1578 kmod_module_unref(m);
1579 }
1580 }
1581
1582 fclose(fp);
1583 *list = l;
1584
1585 return 0;
1586}
1587
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001588/**
1589 * kmod_module_initstate_str:
1590 * @state: the state as returned by kmod_module_get_initstate()
1591 *
1592 * Translate a initstate to a string.
1593 *
1594 * Returns: the string associated to the @state. This string is statically
1595 * allocated, do not free it.
1596 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001597KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1598{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001599 switch (state) {
1600 case KMOD_MODULE_BUILTIN:
1601 return "builtin";
1602 case KMOD_MODULE_LIVE:
1603 return "live";
1604 case KMOD_MODULE_COMING:
1605 return "coming";
1606 case KMOD_MODULE_GOING:
1607 return "going";
1608 default:
1609 return NULL;
1610 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001611}
1612
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001613/**
1614 * kmod_module_get_initstate:
1615 * @mod: kmod module
1616 *
1617 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1618 * /sys filesystem.
1619 *
1620 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1621 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001622KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1623{
1624 char path[PATH_MAX], buf[32];
1625 int fd, err, pathlen;
1626
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001627 if (mod == NULL)
1628 return -ENOENT;
1629
Lucas De Marchi38052742012-02-16 20:43:16 -02001630 if (mod->builtin)
1631 return KMOD_MODULE_BUILTIN;
1632
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001633 pathlen = snprintf(path, sizeof(path),
1634 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001635 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001636 if (fd < 0) {
1637 err = -errno;
1638
Lucas De Marchiddbda022011-12-27 11:40:10 -02001639 DBG(mod->ctx, "could not open '%s': %s\n",
1640 path, strerror(-err));
1641
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001642 if (pathlen > (int)sizeof("/initstate") - 1) {
1643 struct stat st;
1644 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1645 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1646 return KMOD_MODULE_BUILTIN;
1647 }
1648
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001649 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001650 path, strerror(-err));
1651 return err;
1652 }
1653
1654 err = read_str_safe(fd, buf, sizeof(buf));
1655 close(fd);
1656 if (err < 0) {
1657 ERR(mod->ctx, "could not read from '%s': %s\n",
1658 path, strerror(-err));
1659 return err;
1660 }
1661
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001662 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001663 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001664 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001665 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001666 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001667 return KMOD_MODULE_GOING;
1668
1669 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1670 return -EINVAL;
1671}
1672
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001673/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001674 * kmod_module_get_size:
1675 * @mod: kmod module
1676 *
1677 * Get the size of this kmod module as returned by Linux kernel. It reads the
1678 * file /proc/modules to search for this module and get its size.
1679 *
1680 * Returns: the size of this kmod module.
1681 */
1682KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1683{
1684 // FIXME TODO: this should be available from /sys/module/foo
1685 FILE *fp;
1686 char line[4096];
1687 int lineno = 0;
1688 long size = -ENOENT;
1689
1690 if (mod == NULL)
1691 return -ENOENT;
1692
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001693 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001694 if (fp == NULL) {
1695 int err = -errno;
1696 ERR(mod->ctx,
1697 "could not open /proc/modules: %s\n", strerror(errno));
1698 return err;
1699 }
1700
1701 while (fgets(line, sizeof(line), fp)) {
1702 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1703 long value;
1704
1705 lineno++;
1706 if (tok == NULL || !streq(tok, mod->name))
1707 continue;
1708
1709 tok = strtok_r(NULL, " \t", &saveptr);
1710 if (tok == NULL) {
1711 ERR(mod->ctx,
1712 "invalid line format at /proc/modules:%d\n", lineno);
1713 break;
1714 }
1715
1716 value = strtol(tok, &endptr, 10);
1717 if (endptr == tok || *endptr != '\0') {
1718 ERR(mod->ctx,
1719 "invalid line format at /proc/modules:%d\n", lineno);
1720 break;
1721 }
1722
1723 size = value;
1724 break;
1725 }
1726 fclose(fp);
1727 return size;
1728}
1729
1730/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001731 * kmod_module_get_refcnt:
1732 * @mod: kmod module
1733 *
1734 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1735 * /sys filesystem.
1736 *
1737 * Returns: 0 on success or < 0 on failure.
1738 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001739KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1740{
1741 char path[PATH_MAX];
1742 long refcnt;
1743 int fd, err;
1744
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001745 if (mod == NULL)
1746 return -ENOENT;
1747
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001748 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001749 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001750 if (fd < 0) {
1751 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001752 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001753 path, strerror(errno));
1754 return err;
1755 }
1756
1757 err = read_str_long(fd, &refcnt, 10);
1758 close(fd);
1759 if (err < 0) {
1760 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1761 path, strerror(-err));
1762 return err;
1763 }
1764
1765 return (int)refcnt;
1766}
1767
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001768/**
1769 * kmod_module_get_holders:
1770 * @mod: kmod module
1771 *
1772 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1773 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1774 *
1775 * Returns: a new list of kmod modules on success or NULL on failure.
1776 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001777KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1778{
1779 char dname[PATH_MAX];
1780 struct kmod_list *list = NULL;
1781 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001782
1783 if (mod == NULL)
1784 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001785
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001786 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1787
1788 d = opendir(dname);
1789 if (d == NULL) {
1790 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001791 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001792 return NULL;
1793 }
1794
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001795 for (;;) {
1796 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001797 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001798 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001799 int err;
1800
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001801 err = readdir_r(d, &de, &entp);
1802 if (err != 0) {
1803 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1804 mod->name, strerror(-err));
1805 goto fail;
1806 }
1807
1808 if (entp == NULL)
1809 break;
1810
1811 if (de.d_name[0] == '.') {
1812 if (de.d_name[1] == '\0' ||
1813 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001814 continue;
1815 }
1816
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001817 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001818 if (err < 0) {
1819 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001820 de.d_name, strerror(-err));
1821 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001822 }
1823
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001824 l = kmod_list_append(list, holder);
1825 if (l != NULL) {
1826 list = l;
1827 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001828 ERR(mod->ctx, "out of memory\n");
1829 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001830 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001831 }
1832 }
1833
1834 closedir(d);
1835 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001836
1837fail:
1838 closedir(d);
1839 kmod_module_unref_list(list);
1840 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001841}
1842
1843struct kmod_module_section {
1844 unsigned long address;
1845 char name[];
1846};
1847
1848static void kmod_module_section_free(struct kmod_module_section *section)
1849{
1850 free(section);
1851}
1852
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001853/**
1854 * kmod_module_get_sections:
1855 * @mod: kmod module
1856 *
1857 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1858 * structure contained in this list is internal to libkmod and their fields
1859 * can be obtained by calling kmod_module_section_get_name() and
1860 * kmod_module_section_get_address().
1861 *
1862 * After use, free the @list by calling kmod_module_section_free_list().
1863 *
1864 * Returns: a new list of kmod module sections on success or NULL on failure.
1865 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001866KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1867{
1868 char dname[PATH_MAX];
1869 struct kmod_list *list = NULL;
1870 DIR *d;
1871 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001872
1873 if (mod == NULL)
1874 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001875
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001876 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1877
1878 d = opendir(dname);
1879 if (d == NULL) {
1880 ERR(mod->ctx, "could not open '%s': %s\n",
1881 dname, strerror(errno));
1882 return NULL;
1883 }
1884
1885 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001886
1887 for (;;) {
1888 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001889 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001890 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001891 unsigned long address;
1892 size_t namesz;
1893 int fd, err;
1894
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001895 err = readdir_r(d, &de, &entp);
1896 if (err != 0) {
1897 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1898 mod->name, strerror(-err));
1899 goto fail;
1900 }
1901
1902 if (de.d_name[0] == '.') {
1903 if (de.d_name[1] == '\0' ||
1904 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001905 continue;
1906 }
1907
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001908 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001909 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001910 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1911 dname, de.d_name);
1912 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001913 }
1914
1915 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001916 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001917 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001918 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1919 dname, de.d_name);
1920 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001921 }
1922
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001923 namesz = strlen(de.d_name) + 1;
1924 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001925
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001926 if (section == NULL) {
1927 ERR(mod->ctx, "out of memory\n");
1928 goto fail;
1929 }
1930
1931 section->address = address;
1932 memcpy(section->name, de.d_name, namesz);
1933
1934 l = kmod_list_append(list, section);
1935 if (l != NULL) {
1936 list = l;
1937 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001938 ERR(mod->ctx, "out of memory\n");
1939 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001940 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001941 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001942 }
1943
1944 closedir(d);
1945 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001946
1947fail:
1948 closedir(d);
1949 kmod_module_unref_list(list);
1950 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001951}
1952
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001953/**
1954 * kmod_module_section_get_module_name:
1955 * @entry: a list entry representing a kmod module section
1956 *
1957 * Get the name of a kmod module section.
1958 *
1959 * After use, free the @list by calling kmod_module_section_free_list().
1960 *
1961 * Returns: the name of this kmod module section on success or NULL on
1962 * failure. The string is owned by the section, do not free it.
1963 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001964KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1965{
1966 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001967
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001968 if (entry == NULL)
1969 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001970
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001971 section = entry->data;
1972 return section->name;
1973}
1974
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001975/**
1976 * kmod_module_section_get_address:
1977 * @entry: a list entry representing a kmod module section
1978 *
1979 * Get the address of a kmod module section.
1980 *
1981 * After use, free the @list by calling kmod_module_section_free_list().
1982 *
1983 * Returns: the address of this kmod module section on success or ULONG_MAX
1984 * on failure.
1985 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001986KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1987{
1988 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001989
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001990 if (entry == NULL)
1991 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001992
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001993 section = entry->data;
1994 return section->address;
1995}
1996
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001997/**
1998 * kmod_module_section_free_list:
1999 * @list: kmod module section list
2000 *
2001 * Release the resources taken by @list
2002 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002003KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2004{
2005 while (list) {
2006 kmod_module_section_free(list->data);
2007 list = kmod_list_remove(list);
2008 }
2009}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002010
2011struct kmod_module_info {
2012 char *key;
2013 char value[];
2014};
2015
2016static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2017{
2018 struct kmod_module_info *info;
2019
2020 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2021 if (info == NULL)
2022 return NULL;
2023
2024 info->key = (char *)info + sizeof(struct kmod_module_info)
2025 + valuelen + 1;
2026 memcpy(info->key, key, keylen);
2027 info->key[keylen] = '\0';
2028 memcpy(info->value, value, valuelen);
2029 info->value[valuelen] = '\0';
2030 return info;
2031}
2032
2033static void kmod_module_info_free(struct kmod_module_info *info)
2034{
2035 free(info);
2036}
2037
2038/**
2039 * kmod_module_get_info:
2040 * @mod: kmod module
2041 * @list: where to return list of module information. Use
2042 * kmod_module_info_get_key() and
2043 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002044 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002045 *
2046 * Get a list of entries in ELF section ".modinfo", these contain
2047 * alias, license, depends, vermagic and other keys with respective
2048 * values.
2049 *
2050 * After use, free the @list by calling kmod_module_info_free_list().
2051 *
2052 * Returns: 0 on success or < 0 otherwise.
2053 */
2054KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2055{
2056 struct kmod_file *file;
2057 struct kmod_elf *elf;
2058 const char *path;
2059 const void *mem;
2060 char **strings;
2061 size_t size;
2062 int i, count, ret = 0;
2063
2064 if (mod == NULL || list == NULL)
2065 return -ENOENT;
2066
2067 assert(*list == NULL);
2068
2069 path = kmod_module_get_path(mod);
2070 if (path == NULL)
2071 return -ENOENT;
2072
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002073 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002074 if (file == NULL)
2075 return -errno;
2076
2077 size = kmod_file_get_size(file);
2078 mem = kmod_file_get_contents(file);
2079
2080 elf = kmod_elf_new(mem, size);
2081 if (elf == NULL) {
2082 ret = -errno;
2083 goto elf_open_error;
2084 }
2085
2086 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
2087 if (count < 0) {
2088 ret = count;
2089 goto get_strings_error;
2090 }
2091
2092 for (i = 0; i < count; i++) {
2093 struct kmod_module_info *info;
2094 struct kmod_list *n;
2095 const char *key, *value;
2096 size_t keylen, valuelen;
2097
2098 key = strings[i];
2099 value = strchr(key, '=');
2100 if (value == NULL) {
2101 keylen = strlen(key);
2102 valuelen = 0;
2103 } else {
2104 keylen = value - key;
2105 value++;
2106 valuelen = strlen(value);
2107 }
2108
2109 info = kmod_module_info_new(key, keylen, value, valuelen);
2110 if (info == NULL) {
2111 ret = -errno;
2112 kmod_module_info_free_list(*list);
2113 *list = NULL;
2114 goto list_error;
2115 }
2116
2117 n = kmod_list_append(*list, info);
2118 if (n != NULL)
2119 *list = n;
2120 else {
2121 kmod_module_info_free(info);
2122 kmod_module_info_free_list(*list);
2123 *list = NULL;
2124 ret = -ENOMEM;
2125 goto list_error;
2126 }
2127 }
2128 ret = count;
2129
2130list_error:
2131 free(strings);
2132get_strings_error:
2133 kmod_elf_unref(elf);
2134elf_open_error:
2135 kmod_file_unref(file);
2136
2137 return ret;
2138}
2139
2140/**
2141 * kmod_module_info_get_key:
2142 * @entry: a list entry representing a kmod module info
2143 *
2144 * Get the key of a kmod module info.
2145 *
2146 * Returns: the key of this kmod module info on success or NULL on
2147 * failure. The string is owned by the info, do not free it.
2148 */
2149KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2150{
2151 struct kmod_module_info *info;
2152
2153 if (entry == NULL)
2154 return NULL;
2155
2156 info = entry->data;
2157 return info->key;
2158}
2159
2160/**
2161 * kmod_module_info_get_value:
2162 * @entry: a list entry representing a kmod module info
2163 *
2164 * Get the value of a kmod module info.
2165 *
2166 * Returns: the value of this kmod module info on success or NULL on
2167 * failure. The string is owned by the info, do not free it.
2168 */
2169KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2170{
2171 struct kmod_module_info *info;
2172
2173 if (entry == NULL)
2174 return NULL;
2175
2176 info = entry->data;
2177 return info->value;
2178}
2179
2180/**
2181 * kmod_module_info_free_list:
2182 * @list: kmod module info list
2183 *
2184 * Release the resources taken by @list
2185 */
2186KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2187{
2188 while (list) {
2189 kmod_module_info_free(list->data);
2190 list = kmod_list_remove(list);
2191 }
2192}
2193
2194struct kmod_module_version {
2195 uint64_t crc;
2196 char symbol[];
2197};
2198
2199static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2200{
2201 struct kmod_module_version *mv;
2202 size_t symbollen = strlen(symbol) + 1;
2203
2204 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2205 if (mv == NULL)
2206 return NULL;
2207
2208 mv->crc = crc;
2209 memcpy(mv->symbol, symbol, symbollen);
2210 return mv;
2211}
2212
2213static void kmod_module_version_free(struct kmod_module_version *version)
2214{
2215 free(version);
2216}
2217
2218/**
2219 * kmod_module_get_versions:
2220 * @mod: kmod module
2221 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002222 * kmod_module_version_get_symbol() and
2223 * kmod_module_version_get_crc(). Release this list with
2224 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002225 *
2226 * Get a list of entries in ELF section "__versions".
2227 *
2228 * After use, free the @list by calling kmod_module_versions_free_list().
2229 *
2230 * Returns: 0 on success or < 0 otherwise.
2231 */
2232KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2233{
2234 struct kmod_file *file;
2235 struct kmod_elf *elf;
2236 const char *path;
2237 const void *mem;
2238 struct kmod_modversion *versions;
2239 size_t size;
2240 int i, count, ret = 0;
2241
2242 if (mod == NULL || list == NULL)
2243 return -ENOENT;
2244
2245 assert(*list == NULL);
2246
2247 path = kmod_module_get_path(mod);
2248 if (path == NULL)
2249 return -ENOENT;
2250
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002251 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002252 if (file == NULL)
2253 return -errno;
2254
2255 size = kmod_file_get_size(file);
2256 mem = kmod_file_get_contents(file);
2257
2258 elf = kmod_elf_new(mem, size);
2259 if (elf == NULL) {
2260 ret = -errno;
2261 goto elf_open_error;
2262 }
2263
2264 count = kmod_elf_get_modversions(elf, &versions);
2265 if (count < 0) {
2266 ret = count;
2267 goto get_strings_error;
2268 }
2269
2270 for (i = 0; i < count; i++) {
2271 struct kmod_module_version *mv;
2272 struct kmod_list *n;
2273
2274 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2275 if (mv == NULL) {
2276 ret = -errno;
2277 kmod_module_versions_free_list(*list);
2278 *list = NULL;
2279 goto list_error;
2280 }
2281
2282 n = kmod_list_append(*list, mv);
2283 if (n != NULL)
2284 *list = n;
2285 else {
2286 kmod_module_version_free(mv);
2287 kmod_module_versions_free_list(*list);
2288 *list = NULL;
2289 ret = -ENOMEM;
2290 goto list_error;
2291 }
2292 }
2293 ret = count;
2294
2295list_error:
2296 free(versions);
2297get_strings_error:
2298 kmod_elf_unref(elf);
2299elf_open_error:
2300 kmod_file_unref(file);
2301
2302 return ret;
2303}
2304
2305/**
2306 * kmod_module_versions_get_symbol:
2307 * @entry: a list entry representing a kmod module versions
2308 *
2309 * Get the symbol of a kmod module versions.
2310 *
2311 * Returns: the symbol of this kmod module versions on success or NULL
2312 * on failure. The string is owned by the versions, do not free it.
2313 */
2314KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2315{
2316 struct kmod_module_version *version;
2317
2318 if (entry == NULL)
2319 return NULL;
2320
2321 version = entry->data;
2322 return version->symbol;
2323}
2324
2325/**
2326 * kmod_module_version_get_crc:
2327 * @entry: a list entry representing a kmod module version
2328 *
2329 * Get the crc of a kmod module version.
2330 *
2331 * Returns: the crc of this kmod module version on success or NULL on
2332 * failure. The string is owned by the version, do not free it.
2333 */
2334KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2335{
2336 struct kmod_module_version *version;
2337
2338 if (entry == NULL)
2339 return 0;
2340
2341 version = entry->data;
2342 return version->crc;
2343}
2344
2345/**
2346 * kmod_module_versions_free_list:
2347 * @list: kmod module versions list
2348 *
2349 * Release the resources taken by @list
2350 */
2351KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2352{
2353 while (list) {
2354 kmod_module_version_free(list->data);
2355 list = kmod_list_remove(list);
2356 }
2357}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002358
2359struct kmod_module_symbol {
2360 uint64_t crc;
2361 char symbol[];
2362};
2363
2364static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2365{
2366 struct kmod_module_symbol *mv;
2367 size_t symbollen = strlen(symbol) + 1;
2368
2369 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2370 if (mv == NULL)
2371 return NULL;
2372
2373 mv->crc = crc;
2374 memcpy(mv->symbol, symbol, symbollen);
2375 return mv;
2376}
2377
2378static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2379{
2380 free(symbol);
2381}
2382
2383/**
2384 * kmod_module_get_symbols:
2385 * @mod: kmod module
2386 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002387 * kmod_module_symbol_get_symbol() and
2388 * kmod_module_symbol_get_crc(). Release this list with
2389 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002390 *
2391 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2392 *
2393 * After use, free the @list by calling kmod_module_symbols_free_list().
2394 *
2395 * Returns: 0 on success or < 0 otherwise.
2396 */
2397KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2398{
2399 struct kmod_file *file;
2400 struct kmod_elf *elf;
2401 const char *path;
2402 const void *mem;
2403 struct kmod_modversion *symbols;
2404 size_t size;
2405 int i, count, ret = 0;
2406
2407 if (mod == NULL || list == NULL)
2408 return -ENOENT;
2409
2410 assert(*list == NULL);
2411
2412 path = kmod_module_get_path(mod);
2413 if (path == NULL)
2414 return -ENOENT;
2415
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002416 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002417 if (file == NULL)
2418 return -errno;
2419
2420 size = kmod_file_get_size(file);
2421 mem = kmod_file_get_contents(file);
2422
2423 elf = kmod_elf_new(mem, size);
2424 if (elf == NULL) {
2425 ret = -errno;
2426 goto elf_open_error;
2427 }
2428
2429 count = kmod_elf_get_symbols(elf, &symbols);
2430 if (count < 0) {
2431 ret = count;
2432 goto get_strings_error;
2433 }
2434
2435 for (i = 0; i < count; i++) {
2436 struct kmod_module_symbol *mv;
2437 struct kmod_list *n;
2438
2439 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2440 if (mv == NULL) {
2441 ret = -errno;
2442 kmod_module_symbols_free_list(*list);
2443 *list = NULL;
2444 goto list_error;
2445 }
2446
2447 n = kmod_list_append(*list, mv);
2448 if (n != NULL)
2449 *list = n;
2450 else {
2451 kmod_module_symbol_free(mv);
2452 kmod_module_symbols_free_list(*list);
2453 *list = NULL;
2454 ret = -ENOMEM;
2455 goto list_error;
2456 }
2457 }
2458 ret = count;
2459
2460list_error:
2461 free(symbols);
2462get_strings_error:
2463 kmod_elf_unref(elf);
2464elf_open_error:
2465 kmod_file_unref(file);
2466
2467 return ret;
2468}
2469
2470/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002471 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002472 * @entry: a list entry representing a kmod module symbols
2473 *
2474 * Get the symbol of a kmod module symbols.
2475 *
2476 * Returns: the symbol of this kmod module symbols on success or NULL
2477 * on failure. The string is owned by the symbols, do not free it.
2478 */
2479KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2480{
2481 struct kmod_module_symbol *symbol;
2482
2483 if (entry == NULL)
2484 return NULL;
2485
2486 symbol = entry->data;
2487 return symbol->symbol;
2488}
2489
2490/**
2491 * kmod_module_symbol_get_crc:
2492 * @entry: a list entry representing a kmod module symbol
2493 *
2494 * Get the crc of a kmod module symbol.
2495 *
2496 * Returns: the crc of this kmod module symbol on success or NULL on
2497 * failure. The string is owned by the symbol, do not free it.
2498 */
2499KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2500{
2501 struct kmod_module_symbol *symbol;
2502
2503 if (entry == NULL)
2504 return 0;
2505
2506 symbol = entry->data;
2507 return symbol->crc;
2508}
2509
2510/**
2511 * kmod_module_symbols_free_list:
2512 * @list: kmod module symbols list
2513 *
2514 * Release the resources taken by @list
2515 */
2516KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2517{
2518 while (list) {
2519 kmod_module_symbol_free(list->data);
2520 list = kmod_list_remove(list);
2521 }
2522}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002523
2524struct kmod_module_dependency_symbol {
2525 uint64_t crc;
2526 uint8_t bind;
2527 char symbol[];
2528};
2529
2530static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2531{
2532 struct kmod_module_dependency_symbol *mv;
2533 size_t symbollen = strlen(symbol) + 1;
2534
2535 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2536 if (mv == NULL)
2537 return NULL;
2538
2539 mv->crc = crc;
2540 mv->bind = bind;
2541 memcpy(mv->symbol, symbol, symbollen);
2542 return mv;
2543}
2544
2545static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2546{
2547 free(dependency_symbol);
2548}
2549
2550/**
2551 * kmod_module_get_dependency_symbols:
2552 * @mod: kmod module
2553 * @list: where to return list of module dependency_symbols. Use
2554 * kmod_module_dependency_symbol_get_symbol() and
2555 * kmod_module_dependency_symbol_get_crc(). Release this list with
2556 * kmod_module_dependency_symbols_free_list()
2557 *
2558 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2559 *
2560 * After use, free the @list by calling
2561 * kmod_module_dependency_symbols_free_list().
2562 *
2563 * Returns: 0 on success or < 0 otherwise.
2564 */
2565KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2566{
2567 struct kmod_file *file;
2568 struct kmod_elf *elf;
2569 const char *path;
2570 const void *mem;
2571 struct kmod_modversion *symbols;
2572 size_t size;
2573 int i, count, ret = 0;
2574
2575 if (mod == NULL || list == NULL)
2576 return -ENOENT;
2577
2578 assert(*list == NULL);
2579
2580 path = kmod_module_get_path(mod);
2581 if (path == NULL)
2582 return -ENOENT;
2583
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002584 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002585 if (file == NULL)
2586 return -errno;
2587
2588 size = kmod_file_get_size(file);
2589 mem = kmod_file_get_contents(file);
2590
2591 elf = kmod_elf_new(mem, size);
2592 if (elf == NULL) {
2593 ret = -errno;
2594 goto elf_open_error;
2595 }
2596
2597 count = kmod_elf_get_dependency_symbols(elf, &symbols);
2598 if (count < 0) {
2599 ret = count;
2600 goto get_strings_error;
2601 }
2602
2603 for (i = 0; i < count; i++) {
2604 struct kmod_module_dependency_symbol *mv;
2605 struct kmod_list *n;
2606
2607 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2608 symbols[i].bind,
2609 symbols[i].symbol);
2610 if (mv == NULL) {
2611 ret = -errno;
2612 kmod_module_dependency_symbols_free_list(*list);
2613 *list = NULL;
2614 goto list_error;
2615 }
2616
2617 n = kmod_list_append(*list, mv);
2618 if (n != NULL)
2619 *list = n;
2620 else {
2621 kmod_module_dependency_symbol_free(mv);
2622 kmod_module_dependency_symbols_free_list(*list);
2623 *list = NULL;
2624 ret = -ENOMEM;
2625 goto list_error;
2626 }
2627 }
2628 ret = count;
2629
2630list_error:
2631 free(symbols);
2632get_strings_error:
2633 kmod_elf_unref(elf);
2634elf_open_error:
2635 kmod_file_unref(file);
2636
2637 return ret;
2638}
2639
2640/**
2641 * kmod_module_dependency_symbol_get_symbol:
2642 * @entry: a list entry representing a kmod module dependency_symbols
2643 *
2644 * Get the dependency symbol of a kmod module
2645 *
2646 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2647 * on failure. The string is owned by the dependency_symbols, do not free it.
2648 */
2649KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2650{
2651 struct kmod_module_dependency_symbol *dependency_symbol;
2652
2653 if (entry == NULL)
2654 return NULL;
2655
2656 dependency_symbol = entry->data;
2657 return dependency_symbol->symbol;
2658}
2659
2660/**
2661 * kmod_module_dependency_symbol_get_crc:
2662 * @entry: a list entry representing a kmod module dependency_symbol
2663 *
2664 * Get the crc of a kmod module dependency_symbol.
2665 *
2666 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2667 * failure. The string is owned by the dependency_symbol, do not free it.
2668 */
2669KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2670{
2671 struct kmod_module_dependency_symbol *dependency_symbol;
2672
2673 if (entry == NULL)
2674 return 0;
2675
2676 dependency_symbol = entry->data;
2677 return dependency_symbol->crc;
2678}
2679
2680/**
2681 * kmod_module_dependency_symbol_get_bind:
2682 * @entry: a list entry representing a kmod module dependency_symbol
2683 *
2684 * Get the bind type of a kmod module dependency_symbol.
2685 *
2686 * Returns: the bind of this kmod module dependency_symbol on success
2687 * or < 0 on failure.
2688 */
2689KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2690{
2691 struct kmod_module_dependency_symbol *dependency_symbol;
2692
2693 if (entry == NULL)
2694 return 0;
2695
2696 dependency_symbol = entry->data;
2697 return dependency_symbol->bind;
2698}
2699
2700/**
2701 * kmod_module_dependency_symbols_free_list:
2702 * @list: kmod module dependency_symbols list
2703 *
2704 * Release the resources taken by @list
2705 */
2706KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2707{
2708 while (list) {
2709 kmod_module_dependency_symbol_free(list->data);
2710 list = kmod_list_remove(list);
2711 }
2712}