blob: 637b79215f1495edef93de0f0ff5f433e4907fc9 [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 ||
113 state == KMOD_MODULE_COMING ||
114 state == KMOD_MODULE_BUILTIN)
115 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200116
117 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500118}
119
Lucas De Marchi671d4892011-12-05 20:23:05 -0200120int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200121{
122 struct kmod_ctx *ctx = mod->ctx;
123 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200124 const char *dirname;
125 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200126 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200127 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200128 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200129
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200130 if (mod->init.dep)
131 return mod->n_dep;
132 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200133 mod->init.dep = true;
134
135 p = strchr(line, ':');
136 if (p == NULL)
137 return 0;
138
Lucas De Marchi671d4892011-12-05 20:23:05 -0200139 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200140 dirname = kmod_get_dirname(mod->ctx);
141 dirnamelen = strlen(dirname);
142 if (dirnamelen + 2 >= PATH_MAX)
143 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200144
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200145 memcpy(buf, dirname, dirnamelen);
146 buf[dirnamelen] = '/';
147 dirnamelen++;
148 buf[dirnamelen] = '\0';
149
150 if (mod->path == NULL) {
151 const char *str = path_join(line, dirnamelen, buf);
152 if (str == NULL)
153 return 0;
154 mod->path = strdup(str);
155 if (mod->path == NULL)
156 return 0;
157 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200158
Lucas De Marchi7636e722011-12-01 17:56:03 -0200159 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200160 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
161 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200162 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200163 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200164
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200165 path = path_join(p, dirnamelen, buf);
166 if (path == NULL) {
167 ERR(ctx, "could not join path '%s' and '%s'.\n",
168 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200169 goto fail;
170 }
171
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200172 err = kmod_module_new_from_path(ctx, path, &depmod);
173 if (err < 0) {
174 ERR(ctx, "ctx=%p path=%s error=%s\n",
175 ctx, path, strerror(-err));
176 goto fail;
177 }
178
179 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200180
Lucas De Marchib94a7372011-12-26 20:10:49 -0200181 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200182 n++;
183 }
184
185 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
186
187 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200188 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200189 return n;
190
191fail:
192 kmod_module_unref_list(list);
193 mod->init.dep = false;
194 return err;
195}
196
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200197void kmod_module_set_visited(struct kmod_module *mod, bool visited)
198{
199 mod->visited = visited;
200}
201
Lucas De Marchi38052742012-02-16 20:43:16 -0200202void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
203{
204 mod->builtin = builtin;
205}
206
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200207/*
208 * Memory layout with alias:
209 *
210 * struct kmod_module {
211 * hashkey -----.
212 * alias -----. |
213 * name ----. | |
214 * } | | |
215 * name <----------' | |
216 * alias <-----------' |
217 * name\alias <--------'
218 *
219 * Memory layout without alias:
220 *
221 * struct kmod_module {
222 * hashkey ---.
223 * alias -----|----> NULL
224 * name ----. |
225 * } | |
226 * name <----------'-'
227 *
228 * @key is "name\alias" or "name" (in which case alias == NULL)
229 */
230static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
231 const char *name, size_t namelen,
232 const char *alias, size_t aliaslen,
233 struct kmod_module **mod)
234{
235 struct kmod_module *m;
236 size_t keylen;
237
238 m = kmod_pool_get_module(ctx, key);
239 if (m != NULL) {
240 *mod = kmod_module_ref(m);
241 return 0;
242 }
243
244 if (alias == NULL)
245 keylen = namelen;
246 else
247 keylen = namelen + aliaslen + 1;
248
249 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
250 if (m == NULL) {
251 free(m);
252 return -ENOMEM;
253 }
254
255 memset(m, 0, sizeof(*m));
256
257 m->ctx = kmod_ref(ctx);
258 m->name = (char *)m + sizeof(*m);
259 memcpy(m->name, key, keylen + 1);
260 if (alias == NULL) {
261 m->hashkey = m->name;
262 m->alias = NULL;
263 } else {
264 m->name[namelen] = '\0';
265 m->alias = m->name + namelen + 1;
266 m->hashkey = m->name + keylen + 1;
267 memcpy(m->hashkey, key, keylen + 1);
268 }
269
270 m->refcount = 1;
271 kmod_pool_add_module(ctx, m, m->hashkey);
272 *mod = m;
273
274 return 0;
275}
276
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200277/**
278 * kmod_module_new_from_name:
279 * @ctx: kmod library context
280 * @name: name of the module
281 * @mod: where to save the created struct kmod_module
282 *
283 * Create a new struct kmod_module using the module name. @name can not be an
284 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600285 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200286 *
287 * This function is also used internally by many others that return a new
288 * struct kmod_module or a new list of modules.
289 *
290 * The initial refcount is 1, and needs to be decremented to release the
291 * resources of the kmod_module. Since libkmod keeps track of all
292 * kmod_modules created, they are all released upon @ctx destruction too. Do
293 * not unref @ctx before all the desired operations with the returned
294 * kmod_module are done.
295 *
296 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
297 * module name or if memory allocation failed.
298 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200299KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
300 const char *name,
301 struct kmod_module **mod)
302{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200303 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200304 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200305
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200306 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200307 return -ENOENT;
308
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200309 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200310
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200311 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200312}
313
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200314int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
315 const char *name, struct kmod_module **mod)
316{
317 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200318 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200319 size_t namelen = strlen(name);
320 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200321
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200322 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200323 return -ENAMETOOLONG;
324
325 memcpy(key, name, namelen);
326 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200327 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200328
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200329 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200330 if (err < 0)
331 return err;
332
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200333 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200334}
335
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200336/**
337 * kmod_module_new_from_path:
338 * @ctx: kmod library context
339 * @path: path where to find the given module
340 * @mod: where to save the created struct kmod_module
341 *
342 * Create a new struct kmod_module using the module path. @path must be an
343 * existent file with in the filesystem and must be accessible to libkmod.
344 *
345 * The initial refcount is 1, and needs to be decremented to release the
346 * resources of the kmod_module. Since libkmod keeps track of all
347 * kmod_modules created, they are all released upon @ctx destruction too. Do
348 * not unref @ctx before all the desired operations with the returned
349 * kmod_module are done.
350 *
351 * If @path is relative, it's treated as relative to the current working
352 * directory. Otherwise, give an absolute path.
353 *
354 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
355 * it's not a valid file for a kmod_module or if memory allocation failed.
356 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200357KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
358 const char *path,
359 struct kmod_module **mod)
360{
361 struct kmod_module *m;
362 int err;
363 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200364 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200365 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200366 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200367
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200368 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200369 return -ENOENT;
370
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200371 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200372 if (abspath == NULL) {
373 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200374 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200375 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200376
377 err = stat(abspath, &st);
378 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200379 err = -errno;
380 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200381 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200382 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200383 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200384
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200385 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200386 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200387 free(abspath);
388 return -ENOENT;
389 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200390
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200391 m = kmod_pool_get_module(ctx, name);
392 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200393 if (m->path == NULL)
394 m->path = abspath;
395 else if (streq(m->path, abspath))
396 free(abspath);
397 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200398 ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200399 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200400 free(abspath);
401 return -EEXIST;
402 }
403
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200404 *mod = kmod_module_ref(m);
405 return 0;
406 }
407
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200408 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
409 if (err < 0)
410 return err;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200411
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200412 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200413 *mod = m;
414
415 return 0;
416}
417
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200418/**
419 * kmod_module_unref:
420 * @mod: kmod module
421 *
422 * Drop a reference of the kmod module. If the refcount reaches zero, its
423 * resources are released.
424 *
425 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
426 * returns the passed @mod with its refcount decremented.
427 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200428KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
429{
430 if (mod == NULL)
431 return NULL;
432
433 if (--mod->refcount > 0)
434 return mod;
435
436 DBG(mod->ctx, "kmod_module %p released\n", mod);
437
Lucas De Marchi4084c172011-12-15 13:43:22 -0200438 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200439 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200440 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200441 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200442 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200443 free(mod);
444 return NULL;
445}
446
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200447/**
448 * kmod_module_ref:
449 * @mod: kmod module
450 *
451 * Take a reference of the kmod module, incrementing its refcount.
452 *
453 * Returns: the passed @module with its refcount incremented.
454 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200455KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
456{
457 if (mod == NULL)
458 return NULL;
459
460 mod->refcount++;
461
462 return mod;
463}
464
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200465#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
466 do { \
467 if ((_err) < 0) \
468 goto _label_err; \
469 if (*(_list) != NULL) \
470 goto finish; \
471 } while (0)
472
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200473/**
474 * kmod_module_new_from_lookup:
475 * @ctx: kmod library context
476 * @given_alias: alias to look for
477 * @list: an empty list where to save the list of modules matching
478 * @given_alias
479 *
480 * Create a new list of kmod modules using an alias or module name and lookup
481 * libkmod's configuration files and indexes in order to find the module.
482 * Once it's found in one of the places, it stops searching and create the
483 * list of modules that is saved in @list.
484 *
485 * The search order is: 1. aliases in configuration file; 2. module names in
486 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
487 * in modules.alias index.
488 *
489 * The initial refcount is 1, and needs to be decremented to release the
490 * resources of the kmod_module. The returned @list must be released by
491 * calling kmod_module_unref_list(). Since libkmod keeps track of all
492 * kmod_modules created, they are all released upon @ctx destruction too. Do
493 * not unref @ctx before all the desired operations with the returned list are
494 * completed.
495 *
496 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
497 * methods failed, which is basically due to memory allocation fail. If module
498 * is not found, it still returns 0, but @list is an empty list.
499 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200500KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200501 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200502 struct kmod_list **list)
503{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200504 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200505 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200506
Lucas De Marchi4308b172011-12-13 10:26:04 -0200507 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200508 return -ENOENT;
509
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200510 if (list == NULL || *list != NULL) {
511 ERR(ctx, "An empty list is needed to create lookup\n");
512 return -ENOSYS;
513 }
514
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200515 if (alias_normalize(given_alias, alias, NULL) < 0) {
516 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200517 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200518 }
519
520 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200521
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200522 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200523 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200524 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200525
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200526 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200527 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
528 CHECK_ERR_AND_FINISH(err, fail, list, finish);
529
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200530 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200531 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
532 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200533
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200534 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200535 err = kmod_lookup_alias_from_commands(ctx, alias, list);
536 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200537
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200538 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200539 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
540 CHECK_ERR_AND_FINISH(err, fail, list, finish);
541
Lucas De Marchi38052742012-02-16 20:43:16 -0200542 DBG(ctx, "lookup modules.builtin %s\n", alias);
543 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
544 CHECK_ERR_AND_FINISH(err, fail, list, finish);
545
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200546finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200547 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200548 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200549fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200550 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200551 kmod_module_unref_list(*list);
552 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200553 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200554}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200555#undef CHECK_ERR_AND_FINISH
556
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200557/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200558 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200559 * @list: list of kmod modules
560 *
561 * Drop a reference of each kmod module in @list and releases the resources
562 * taken by the list itself.
563 *
564 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
565 * returns the passed @mod with its refcount decremented.
566 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200567KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
568{
569 for (; list != NULL; list = kmod_list_remove(list))
570 kmod_module_unref(list->data);
571
572 return 0;
573}
574
Lucas De Marchi0d467432011-12-31 11:15:52 -0200575/**
576 * kmod_module_get_filtered_blacklist:
577 * @ctx: kmod library context
578 * @input: list of kmod_module to be filtered with blacklist
579 * @output: where to save the new list
580 *
581 * Given a list @input, this function filter it out with config's blacklist
582 * ans save it in @output.
583 *
584 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
585 * list.
586 */
587KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
588 const struct kmod_list *input,
589 struct kmod_list **output)
590{
591 const struct kmod_list *li;
592 const struct kmod_list *blacklist;
593
594 if (ctx == NULL || output == NULL)
595 return -ENOENT;
596
597 *output = NULL;
598 if (input == NULL)
599 return 0;
600
601 blacklist = kmod_get_blacklists(ctx);
602 kmod_list_foreach(li, input) {
603 struct kmod_module *mod = li->data;
604 const struct kmod_list *lb;
605 struct kmod_list *node;
606 bool filtered = false;
607
608 kmod_list_foreach(lb, blacklist) {
609 const char *name = lb->data;
610
Lucas De Marchi4926cb52011-12-31 11:21:52 -0200611 if (streq(name, mod->name)) {
Lucas De Marchi0d467432011-12-31 11:15:52 -0200612 filtered = true;
613 break;
614 }
615 }
616
617 if (filtered)
618 continue;
619
620 node = kmod_list_append(*output, mod);
621 if (node == NULL)
622 goto fail;
623
624 *output = node;
625 kmod_module_ref(mod);
626 }
627
628 return 0;
629
630fail:
631 kmod_module_unref_list(*output);
632 *output = NULL;
633 return -ENOMEM;
634}
635
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200636static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
637{
638 if (!mod->init.dep) {
639 /* lazy init */
640 char *line = kmod_search_moddep(mod->ctx, mod->name);
641
642 if (line == NULL)
643 return NULL;
644
645 kmod_module_parse_depline((struct kmod_module *)mod, line);
646 free(line);
647
648 if (!mod->init.dep)
649 return NULL;
650 }
651
652 return mod->dep;
653}
654
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200655/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200656 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200657 * @mod: kmod module
658 *
659 * Search the modules.dep index to find the dependencies of the given @mod.
660 * The result is cached in @mod, so subsequent calls to this function will
661 * return the already searched list of modules.
662 *
663 * Returns: NULL on failure or if there are any dependencies. Otherwise it
664 * returns a list of kmod modules that can be released by calling
665 * kmod_module_unref_list().
666 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200667KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200668{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200669 struct kmod_list *l, *l_new, *list_new = NULL;
670
671 if (mod == NULL)
672 return NULL;
673
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200674 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200675
676 kmod_list_foreach(l, mod->dep) {
677 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
678 if (l_new == NULL) {
679 kmod_module_unref(l->data);
680 goto fail;
681 }
682
683 list_new = l_new;
684 }
685
686 return list_new;
687
688fail:
689 ERR(mod->ctx, "out of memory\n");
690 kmod_module_unref_list(list_new);
691 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200692}
693
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200694/**
695 * kmod_module_get_module:
696 * @entry: an entry in a list of kmod modules.
697 *
698 * Get the kmod module of this @entry in the list, increasing its refcount.
699 * After it's used, unref it. Since the refcount is incremented upon return,
700 * you still have to call kmod_module_unref_list() to release the list of kmod
701 * modules.
702 *
703 * Returns: NULL on failure or the kmod_module contained in this list entry
704 * with its refcount incremented.
705 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200706KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200707{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200708 if (entry == NULL)
709 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200710
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200711 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200712}
713
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200714/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200715 * kmod_module_get_name:
716 * @mod: kmod module
717 *
718 * Get the name of this kmod module. Name is always available, independently
719 * if it was created by kmod_module_new_from_name() or another function and
720 * it's always normalized (dashes are replaced with underscores).
721 *
722 * Returns: the name of this kmod module.
723 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200724KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200725{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200726 if (mod == NULL)
727 return NULL;
728
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200729 return mod->name;
730}
731
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200732/**
733 * kmod_module_get_path:
734 * @mod: kmod module
735 *
736 * Get the path of this kmod module. If this kmod module was not created by
737 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200738 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200739 *
740 * Returns: the path of this kmod module or NULL if such information is not
741 * available.
742 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200743KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200744{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200745 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200746
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200747 if (mod == NULL)
748 return NULL;
749
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200750 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200751
Lucas De Marchie005fac2011-12-08 10:42:34 -0200752 if (mod->path != NULL)
753 return mod->path;
754 if (mod->init.dep)
755 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200756
Lucas De Marchie005fac2011-12-08 10:42:34 -0200757 /* lazy init */
758 line = kmod_search_moddep(mod->ctx, mod->name);
759 if (line == NULL)
760 return NULL;
761
762 kmod_module_parse_depline((struct kmod_module *) mod, line);
763 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200764
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200765 return mod->path;
766}
767
768
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200769extern long delete_module(const char *name, unsigned int flags);
770
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200771/**
772 * kmod_module_remove_module:
773 * @mod: kmod module
774 * @flags: flags to pass to Linux kernel when removing the module
775 *
776 * Remove a module from Linux kernel.
777 *
778 * Returns: 0 on success or < 0 on failure.
779 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200780KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
781 unsigned int flags)
782{
783 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200784
785 if (mod == NULL)
786 return -ENOENT;
787
788 /* Filter out other flags */
789 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
790
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200791 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200792 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200793 err = -errno;
794 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200795 }
796
Lucas De Marchiba998b92012-01-11 00:08:14 -0200797 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200798}
799
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200800extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200801
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200802/**
803 * kmod_module_insert_module:
804 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200805 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
806 * behavior of this function.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200807 * @options: module's options to pass to Linux Kernel.
808 *
809 * Insert a module in Linux kernel. It opens the file pointed by @mod,
810 * mmap'ing it and passing to kernel.
811 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200812 * Returns: 0 on success or < 0 on failure. If module is already loaded it
813 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200814 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200815KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200816 unsigned int flags,
817 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200818{
819 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200820 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200821 off_t size;
822 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200823 struct kmod_elf *elf = NULL;
824 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200825 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200826
827 if (mod == NULL)
828 return -ENOENT;
829
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200830 path = kmod_module_get_path(mod);
831 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500832 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200833 return -ENOSYS;
834 }
835
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200836 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200837 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200838 err = -errno;
839 return err;
840 }
841
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200842 size = kmod_file_get_size(file);
843 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200844
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200845 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
846 elf = kmod_elf_new(mem, size);
847 if (elf == NULL) {
848 err = -errno;
849 goto elf_failed;
850 }
851
852 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
853 err = kmod_elf_strip_section(elf, "__versions");
854 if (err < 0)
855 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
856 }
857
858 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
859 err = kmod_elf_strip_vermagic(elf);
860 if (err < 0)
861 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
862 }
863
864 mem = kmod_elf_get_memory(elf);
865 }
866
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200867 err = init_module(mem, size, args);
Lucas De Marchibbf59322011-12-30 14:13:33 -0200868 if (err < 0) {
869 err = -errno;
870 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
871 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200872
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200873 if (elf != NULL)
874 kmod_elf_unref(elf);
875elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200876 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200877
878 return err;
879}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200880
Lucas De Marchiddbda022011-12-27 11:40:10 -0200881static bool module_is_blacklisted(struct kmod_module *mod)
882{
883 struct kmod_ctx *ctx = mod->ctx;
884 const struct kmod_list *bl = kmod_get_blacklists(ctx);
885 const struct kmod_list *l;
886
887 kmod_list_foreach(l, bl) {
888 const char *modname = kmod_blacklist_get_modname(l);
889
890 if (streq(modname, mod->name))
891 return true;
892 }
893
894 return false;
895}
896
Lucas De Marchiddbda022011-12-27 11:40:10 -0200897static int command_do(struct kmod_module *mod, const char *type,
898 const char *cmd)
899{
900 const char *modname = kmod_module_get_name(mod);
901 int err;
902
903 DBG(mod->ctx, "%s %s\n", type, cmd);
904
905 setenv("MODPROBE_MODULE", modname, 1);
906 err = system(cmd);
907 unsetenv("MODPROBE_MODULE");
908
909 if (err == -1 || WEXITSTATUS(err)) {
910 ERR(mod->ctx, "Error running %s command for %s\n",
911 type, modname);
912 if (err != -1)
913 err = -WEXITSTATUS(err);
914 }
915
916 return err;
917}
918
Lucas De Marchib1a51252012-01-29 01:49:09 -0200919struct probe_insert_cb {
920 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
921 void *data;
922};
923
Lucas De Marchiddbda022011-12-27 11:40:10 -0200924static int module_do_install_commands(struct kmod_module *mod,
925 const char *options,
926 struct probe_insert_cb *cb)
927{
928 const char *command = kmod_module_get_install_commands(mod);
929 char *p, *cmd;
930 int err;
931 size_t cmdlen, options_len, varlen;
932
933 assert(command);
934
935 if (options == NULL)
936 options = "";
937
938 options_len = strlen(options);
939 cmdlen = strlen(command);
940 varlen = sizeof("$CMDLINE_OPTS") - 1;
941
942 cmd = memdup(command, cmdlen + 1);
943 if (cmd == NULL)
944 return -ENOMEM;
945
946 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
947 size_t prefixlen = p - cmd;
948 size_t suffixlen = cmdlen - prefixlen - varlen;
949 size_t slen = cmdlen - varlen + options_len;
950 char *suffix = p + varlen;
951 char *s = malloc(slen + 1);
952 if (s == NULL) {
953 free(cmd);
954 return -ENOMEM;
955 }
956 memcpy(s, cmd, p - cmd);
957 memcpy(s + prefixlen, options, options_len);
958 memcpy(s + prefixlen + options_len, suffix, suffixlen);
959 s[slen] = '\0';
960
961 free(cmd);
962 cmd = s;
963 cmdlen = slen;
964 }
965
966 if (cb->run_install != NULL)
967 err = cb->run_install(mod, cmd, cb->data);
968 else
969 err = command_do(mod, "install", cmd);
970
971 free(cmd);
972
973 return err;
974}
975
Lucas De Marchiddbda022011-12-27 11:40:10 -0200976static char *module_options_concat(const char *opt, const char *xopt)
977{
978 // TODO: we might need to check if xopt overrides options on opt
979 size_t optlen = opt == NULL ? 0 : strlen(opt);
980 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
981 char *r;
982
983 if (optlen == 0 && xoptlen == 0)
984 return NULL;
985
986 r = malloc(optlen + xoptlen + 2);
987
988 if (opt != NULL) {
989 memcpy(r, opt, optlen);
990 r[optlen] = ' ';
991 optlen++;
992 }
993
994 if (xopt != NULL)
995 memcpy(r + optlen, xopt, xoptlen);
996
997 r[optlen + xoptlen] = '\0';
998
999 return r;
1000}
1001
Lucas De Marchib1a51252012-01-29 01:49:09 -02001002static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001003 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001004 struct kmod_list **list);
1005
1006/* re-entrant */
1007static int __kmod_module_fill_softdep(struct kmod_module *mod,
1008 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001009{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001010 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001011 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001012
1013 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001014 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001015 ERR(mod->ctx, "could not get softdep: %s\n",
1016 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001017 goto fail;
1018 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001019
Lucas De Marchib1a51252012-01-29 01:49:09 -02001020 kmod_list_foreach(l, pre) {
1021 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001022 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001023 if (err < 0)
1024 goto fail;
1025 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001026
Lucas De Marchib1a51252012-01-29 01:49:09 -02001027 l = kmod_list_append(*list, kmod_module_ref(mod));
1028 if (l == NULL) {
1029 kmod_module_unref(mod);
1030 err = -ENOMEM;
1031 goto fail;
1032 }
1033 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001034 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001035
Lucas De Marchib1a51252012-01-29 01:49:09 -02001036 kmod_list_foreach(l, post) {
1037 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001038 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001039 if (err < 0)
1040 goto fail;
1041 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001042
Lucas De Marchib1a51252012-01-29 01:49:09 -02001043fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001044 kmod_module_unref_list(pre);
1045 kmod_module_unref_list(post);
1046
1047 return err;
1048}
1049
Lucas De Marchib1a51252012-01-29 01:49:09 -02001050/* re-entrant */
1051static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001052 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001053 struct kmod_list **list)
1054{
1055 struct kmod_list *dep, *l;
1056 int err = 0;
1057
1058 if (mod->visited) {
1059 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1060 mod->name);
1061 return 0;
1062 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001063 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001064
1065 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001066 kmod_list_foreach(l, dep) {
1067 struct kmod_module *m = l->data;
1068 err = __kmod_module_fill_softdep(m, list);
1069 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001070 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001071 }
1072
Lucas De Marchi89e92482012-01-29 02:35:46 -02001073 if (ignorecmd) {
1074 l = kmod_list_append(*list, kmod_module_ref(mod));
1075 if (l == NULL) {
1076 kmod_module_unref(mod);
1077 err = -ENOMEM;
1078 goto finish;
1079 }
1080 *list = l;
1081 mod->ignorecmd = true;
1082 } else
1083 err = __kmod_module_fill_softdep(mod, list);
1084
Lucas De Marchib1a51252012-01-29 01:49:09 -02001085finish:
1086 kmod_module_unref_list(dep);
1087 return err;
1088}
1089
1090static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001091 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001092 struct kmod_list **list)
1093{
1094 int err;
1095
1096 assert(mod != NULL);
1097 assert(list != NULL && *list == NULL);
1098
1099 /*
1100 * Make sure we don't get screwed by previous calls to this function
1101 */
1102 kmod_set_modules_visited(mod->ctx, false);
1103
Lucas De Marchi89e92482012-01-29 02:35:46 -02001104 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001105 if (err < 0) {
1106 kmod_module_unref_list(*list);
1107 *list = NULL;
1108 }
1109
1110 return err;
1111}
1112
Lucas De Marchiddbda022011-12-27 11:40:10 -02001113/**
1114 * kmod_module_probe_insert_module:
1115 * @mod: kmod module
1116 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1117 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001118 * @extra_options: module's options to pass to Linux Kernel. It applies only
1119 * to @mod, not to its dependencies.
1120 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001121 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001122 * @print_action: function to call with the action being taken (install or
1123 * insmod). It's useful for tools like modprobe when running with verbose
1124 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001125 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001126 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001127 * install commands and applying blacklist.
1128 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001129 * If @run_install is NULL, this function will fork and exec by calling
1130 * system(3). Don't pass a NULL argument in @run_install if your binary is
1131 * setuid/setgid (see warning in system(3)). If you need control over the
1132 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001133 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001134 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1135 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001136 */
1137KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1138 unsigned int flags, const char *extra_options,
1139 int (*run_install)(struct kmod_module *m,
1140 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001141 const void *data,
1142 void (*print_action)(struct kmod_module *m,
1143 bool install,
1144 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001145{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001146 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001147 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001148 int err;
1149
1150 if (mod == NULL)
1151 return -ENOENT;
1152
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001153 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1154 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001155 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001156 return -EEXIST;
1157 else
1158 return 0;
1159 }
1160
Lucas De Marchib1a51252012-01-29 01:49:09 -02001161 err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
1162 KMOD_PROBE_APPLY_BLACKLIST_ALL);
1163 if (err != 0) {
1164 if (module_is_blacklisted(mod))
1165 return err;
1166 }
1167
Lucas De Marchi89e92482012-01-29 02:35:46 -02001168 err = kmod_module_get_probe_list(mod,
1169 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001170 if (err < 0)
1171 return err;
1172
1173 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1174 struct kmod_list *filtered = NULL;
1175
1176 err = kmod_module_get_filtered_blacklist(mod->ctx,
1177 list, &filtered);
1178 if (err < 0)
1179 return err;
1180
1181 kmod_module_unref_list(list);
1182 if (filtered == NULL)
1183 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1184
1185 list = filtered;
1186 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001187
1188 cb.run_install = run_install;
1189 cb.data = (void *) data;
1190
Lucas De Marchib1a51252012-01-29 01:49:09 -02001191 kmod_list_foreach(l, list) {
1192 struct kmod_module *m = l->data;
1193 const char *moptions = kmod_module_get_options(m);
1194 const char *cmd = kmod_module_get_install_commands(m);
1195 char *options = module_options_concat(moptions,
1196 m == mod ? extra_options : NULL);
1197
Lucas De Marchi89e92482012-01-29 02:35:46 -02001198 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001199 if (print_action != NULL)
1200 print_action(m, true, options ?: "");
1201
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001202 if (!(flags & KMOD_PROBE_DRY_RUN))
1203 err = module_do_install_commands(m, options,
1204 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001205 } else {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001206 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1207 && module_is_inkernel(m)) {
Lucas De Marchi7c10c692012-01-30 18:54:45 -02001208 DBG(mod->ctx, "Ignoring module '%s': "
1209 "already loaded\n", m->name);
Lucas De Marchi08600ee2012-02-06 12:52:27 -02001210 err = -EEXIST;
1211 goto finish_module;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001212 }
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001213 if (print_action != NULL)
1214 print_action(m, false, options ?: "");
1215
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001216 if (!(flags & KMOD_PROBE_DRY_RUN))
1217 err = kmod_module_insert_module(m, flags,
1218 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001219 }
1220
Lucas De Marchi08600ee2012-02-06 12:52:27 -02001221finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001222 free(options);
1223
1224 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001225 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001226 * already loaded and the module being loaded is not a softdep
1227 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001228 *
1229 * We need to check here because of race conditions. We
1230 * checked first if module was already loaded but it may have
1231 * been loaded between the check and the moment we try to
1232 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001233 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001234 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001235 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001236 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001237
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001238 if (err == -EEXIST)
1239 err = 0;
1240 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001241 break;
1242 }
1243
1244 kmod_module_unref_list(list);
1245 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001246}
1247
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001248/**
1249 * kmod_module_get_options:
1250 * @mod: kmod module
1251 *
1252 * Get options of this kmod module. Options come from the configuration file
1253 * and are cached in @mod. The first call to this function will search for
1254 * this module in configuration and subsequent calls return the cached string.
1255 *
1256 * Returns: a string with all the options separated by spaces. This string is
1257 * owned by @mod, do not free it.
1258 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001259KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1260{
1261 if (mod == NULL)
1262 return NULL;
1263
1264 if (!mod->init.options) {
1265 /* lazy init */
1266 struct kmod_module *m = (struct kmod_module *)mod;
1267 const struct kmod_list *l, *ctx_options;
1268 char *opts = NULL;
1269 size_t optslen = 0;
1270
1271 ctx_options = kmod_get_options(mod->ctx);
1272
1273 kmod_list_foreach(l, ctx_options) {
1274 const char *modname = kmod_option_get_modname(l);
1275 const char *str;
1276 size_t len;
1277 void *tmp;
1278
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001279 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1280 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1281 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001282 continue;
1283
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001284 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 -02001285 str = kmod_option_get_options(l);
1286 len = strlen(str);
1287 if (len < 1)
1288 continue;
1289
1290 tmp = realloc(opts, optslen + len + 2);
1291 if (tmp == NULL) {
1292 free(opts);
1293 goto failed;
1294 }
1295
1296 opts = tmp;
1297
1298 if (optslen > 0) {
1299 opts[optslen] = ' ';
1300 optslen++;
1301 }
1302
1303 memcpy(opts + optslen, str, len);
1304 optslen += len;
1305 opts[optslen] = '\0';
1306 }
1307
1308 m->init.options = true;
1309 m->options = opts;
1310 }
1311
1312 return mod->options;
1313
1314failed:
1315 ERR(mod->ctx, "out of memory\n");
1316 return NULL;
1317}
1318
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001319/**
1320 * kmod_module_get_install_commands:
1321 * @mod: kmod module
1322 *
1323 * Get install commands for this kmod module. Install commands come from the
1324 * configuration file and are cached in @mod. The first call to this function
1325 * will search for this module in configuration and subsequent calls return
1326 * the cached string. The install commands are returned as they were in the
1327 * configuration, concatenated by ';'. No other processing is made in this
1328 * string.
1329 *
1330 * Returns: a string with all install commands separated by semicolons. This
1331 * string is owned by @mod, do not free it.
1332 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001333KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1334{
1335 if (mod == NULL)
1336 return NULL;
1337
1338 if (!mod->init.install_commands) {
1339 /* lazy init */
1340 struct kmod_module *m = (struct kmod_module *)mod;
1341 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001342
1343 ctx_install_commands = kmod_get_install_commands(mod->ctx);
1344
1345 kmod_list_foreach(l, ctx_install_commands) {
1346 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001347
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001348 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001349 continue;
1350
Lucas De Marchi60f67602011-12-16 03:33:26 -02001351 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001352
Lucas De Marchi60f67602011-12-16 03:33:26 -02001353 /*
1354 * find only the first command, as modprobe from
1355 * module-init-tools does
1356 */
1357 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001358 }
1359
1360 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001361 }
1362
1363 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001364}
1365
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001366void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1367{
1368 mod->init.install_commands = true;
1369 mod->install_commands = cmd;
1370}
1371
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001372static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1373{
1374 struct kmod_list *ret = NULL;
1375 unsigned i;
1376
1377 for (i = 0; i < count; i++) {
1378 const char *depname = array[i];
1379 struct kmod_list *lst = NULL;
1380 int err;
1381
1382 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1383 if (err < 0) {
1384 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1385 continue;
1386 } else if (lst != NULL)
1387 ret = kmod_list_append_list(ret, lst);
1388 }
1389 return ret;
1390}
1391
1392/**
1393 * kmod_module_get_softdeps:
1394 * @mod: kmod module
1395 * @pre: where to save the list of preceding soft dependencies.
1396 * @post: where to save the list of post soft dependencies.
1397 *
1398 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001399 * from configuration file and are not cached in @mod because it may include
1400 * dependency cycles that would make we leak kmod_module. Any call
1401 * to this function will search for this module in configuration, allocate a
1402 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001403 *
1404 * Both @pre and @post are newly created list of kmod_module and
1405 * should be unreferenced with kmod_module_unref_list().
1406 *
1407 * Returns: 0 on success or < 0 otherwise.
1408 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001409KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1410 struct kmod_list **pre,
1411 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001412{
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001413 const struct kmod_list *l, *ctx_softdeps;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001414
1415 if (mod == NULL || pre == NULL || post == NULL)
1416 return -ENOENT;
1417
1418 assert(*pre == NULL);
1419 assert(*post == NULL);
1420
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001421 ctx_softdeps = kmod_get_softdeps(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001422
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001423 kmod_list_foreach(l, ctx_softdeps) {
1424 const char *modname = kmod_softdep_get_name(l);
1425 const char * const *array;
1426 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001427
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001428 if (fnmatch(modname, mod->name, 0) != 0)
1429 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001430
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001431 array = kmod_softdep_get_pre(l, &count);
1432 *pre = lookup_softdep(mod->ctx, array, count);
1433 array = kmod_softdep_get_post(l, &count);
1434 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001435
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001436 /*
1437 * find only the first command, as modprobe from
1438 * module-init-tools does
1439 */
1440 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001441 }
1442
1443 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001444}
1445
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001446/**
1447 * kmod_module_get_remove_commands:
1448 * @mod: kmod module
1449 *
1450 * Get remove commands for this kmod module. Remove commands come from the
1451 * configuration file and are cached in @mod. The first call to this function
1452 * will search for this module in configuration and subsequent calls return
1453 * the cached string. The remove commands are returned as they were in the
1454 * configuration, concatenated by ';'. No other processing is made in this
1455 * string.
1456 *
1457 * Returns: a string with all remove commands separated by semicolons. This
1458 * string is owned by @mod, do not free it.
1459 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001460KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1461{
1462 if (mod == NULL)
1463 return NULL;
1464
1465 if (!mod->init.remove_commands) {
1466 /* lazy init */
1467 struct kmod_module *m = (struct kmod_module *)mod;
1468 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001469
1470 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1471
1472 kmod_list_foreach(l, ctx_remove_commands) {
1473 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001474
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001475 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001476 continue;
1477
Lucas De Marchi60f67602011-12-16 03:33:26 -02001478 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001479
Lucas De Marchi60f67602011-12-16 03:33:26 -02001480 /*
1481 * find only the first command, as modprobe from
1482 * module-init-tools does
1483 */
1484 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001485 }
1486
1487 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001488 }
1489
1490 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001491}
1492
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001493void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1494{
1495 mod->init.remove_commands = true;
1496 mod->remove_commands = cmd;
1497}
1498
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001499/**
1500 * SECTION:libkmod-loaded
1501 * @short_description: currently loaded modules
1502 *
1503 * Information about currently loaded modules, as reported by Linux kernel.
1504 * These information are not cached by libkmod and are always read from /sys
1505 * and /proc/modules.
1506 */
1507
1508/**
1509 * kmod_module_new_from_loaded:
1510 * @ctx: kmod library context
1511 * @list: where to save the list of loaded modules
1512 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001513 * Create a new list of kmod modules with all modules currently loaded in
1514 * kernel. It uses /proc/modules to get the names of loaded modules and to
1515 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1516 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001517 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001518 * The initial refcount is 1, and needs to be decremented to release the
1519 * resources of the kmod_module. The returned @list must be released by
1520 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1521 * kmod_modules created, they are all released upon @ctx destruction too. Do
1522 * not unref @ctx before all the desired operations with the returned list are
1523 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001524 *
1525 * Returns: 0 on success or < 0 on error.
1526 */
1527KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1528 struct kmod_list **list)
1529{
1530 struct kmod_list *l = NULL;
1531 FILE *fp;
1532 char line[4096];
1533
1534 if (ctx == NULL || list == NULL)
1535 return -ENOENT;
1536
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001537 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001538 if (fp == NULL) {
1539 int err = -errno;
1540 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1541 return err;
1542 }
1543
1544 while (fgets(line, sizeof(line), fp)) {
1545 struct kmod_module *m;
1546 struct kmod_list *node;
1547 int err;
1548 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1549
1550 err = kmod_module_new_from_name(ctx, name, &m);
1551 if (err < 0) {
1552 ERR(ctx, "could not get module from name '%s': %s\n",
1553 name, strerror(-err));
1554 continue;
1555 }
1556
1557 node = kmod_list_append(l, m);
1558 if (node)
1559 l = node;
1560 else {
1561 ERR(ctx, "out of memory\n");
1562 kmod_module_unref(m);
1563 }
1564 }
1565
1566 fclose(fp);
1567 *list = l;
1568
1569 return 0;
1570}
1571
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001572/**
1573 * kmod_module_initstate_str:
1574 * @state: the state as returned by kmod_module_get_initstate()
1575 *
1576 * Translate a initstate to a string.
1577 *
1578 * Returns: the string associated to the @state. This string is statically
1579 * allocated, do not free it.
1580 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001581KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1582{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001583 switch (state) {
1584 case KMOD_MODULE_BUILTIN:
1585 return "builtin";
1586 case KMOD_MODULE_LIVE:
1587 return "live";
1588 case KMOD_MODULE_COMING:
1589 return "coming";
1590 case KMOD_MODULE_GOING:
1591 return "going";
1592 default:
1593 return NULL;
1594 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001595}
1596
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001597/**
1598 * kmod_module_get_initstate:
1599 * @mod: kmod module
1600 *
1601 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1602 * /sys filesystem.
1603 *
1604 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1605 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001606KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1607{
1608 char path[PATH_MAX], buf[32];
1609 int fd, err, pathlen;
1610
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001611 if (mod == NULL)
1612 return -ENOENT;
1613
Lucas De Marchi38052742012-02-16 20:43:16 -02001614 if (mod->builtin)
1615 return KMOD_MODULE_BUILTIN;
1616
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001617 pathlen = snprintf(path, sizeof(path),
1618 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001619 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001620 if (fd < 0) {
1621 err = -errno;
1622
Lucas De Marchiddbda022011-12-27 11:40:10 -02001623 DBG(mod->ctx, "could not open '%s': %s\n",
1624 path, strerror(-err));
1625
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001626 if (pathlen > (int)sizeof("/initstate") - 1) {
1627 struct stat st;
1628 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1629 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1630 return KMOD_MODULE_BUILTIN;
1631 }
1632
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001633 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001634 path, strerror(-err));
1635 return err;
1636 }
1637
1638 err = read_str_safe(fd, buf, sizeof(buf));
1639 close(fd);
1640 if (err < 0) {
1641 ERR(mod->ctx, "could not read from '%s': %s\n",
1642 path, strerror(-err));
1643 return err;
1644 }
1645
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001646 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001647 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001648 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001649 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001650 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001651 return KMOD_MODULE_GOING;
1652
1653 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1654 return -EINVAL;
1655}
1656
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001657/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001658 * kmod_module_get_size:
1659 * @mod: kmod module
1660 *
1661 * Get the size of this kmod module as returned by Linux kernel. It reads the
1662 * file /proc/modules to search for this module and get its size.
1663 *
1664 * Returns: the size of this kmod module.
1665 */
1666KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1667{
1668 // FIXME TODO: this should be available from /sys/module/foo
1669 FILE *fp;
1670 char line[4096];
1671 int lineno = 0;
1672 long size = -ENOENT;
1673
1674 if (mod == NULL)
1675 return -ENOENT;
1676
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001677 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001678 if (fp == NULL) {
1679 int err = -errno;
1680 ERR(mod->ctx,
1681 "could not open /proc/modules: %s\n", strerror(errno));
1682 return err;
1683 }
1684
1685 while (fgets(line, sizeof(line), fp)) {
1686 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1687 long value;
1688
1689 lineno++;
1690 if (tok == NULL || !streq(tok, mod->name))
1691 continue;
1692
1693 tok = strtok_r(NULL, " \t", &saveptr);
1694 if (tok == NULL) {
1695 ERR(mod->ctx,
1696 "invalid line format at /proc/modules:%d\n", lineno);
1697 break;
1698 }
1699
1700 value = strtol(tok, &endptr, 10);
1701 if (endptr == tok || *endptr != '\0') {
1702 ERR(mod->ctx,
1703 "invalid line format at /proc/modules:%d\n", lineno);
1704 break;
1705 }
1706
1707 size = value;
1708 break;
1709 }
1710 fclose(fp);
1711 return size;
1712}
1713
1714/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001715 * kmod_module_get_refcnt:
1716 * @mod: kmod module
1717 *
1718 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1719 * /sys filesystem.
1720 *
1721 * Returns: 0 on success or < 0 on failure.
1722 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001723KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1724{
1725 char path[PATH_MAX];
1726 long refcnt;
1727 int fd, err;
1728
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001729 if (mod == NULL)
1730 return -ENOENT;
1731
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001732 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001733 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001734 if (fd < 0) {
1735 err = -errno;
1736 ERR(mod->ctx, "could not open '%s': %s\n",
1737 path, strerror(errno));
1738 return err;
1739 }
1740
1741 err = read_str_long(fd, &refcnt, 10);
1742 close(fd);
1743 if (err < 0) {
1744 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1745 path, strerror(-err));
1746 return err;
1747 }
1748
1749 return (int)refcnt;
1750}
1751
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001752/**
1753 * kmod_module_get_holders:
1754 * @mod: kmod module
1755 *
1756 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1757 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1758 *
1759 * Returns: a new list of kmod modules on success or NULL on failure.
1760 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001761KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1762{
1763 char dname[PATH_MAX];
1764 struct kmod_list *list = NULL;
1765 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001766
1767 if (mod == NULL)
1768 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001769
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001770 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1771
1772 d = opendir(dname);
1773 if (d == NULL) {
1774 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001775 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001776 return NULL;
1777 }
1778
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001779 for (;;) {
1780 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001781 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001782 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001783 int err;
1784
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001785 err = readdir_r(d, &de, &entp);
1786 if (err != 0) {
1787 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1788 mod->name, strerror(-err));
1789 goto fail;
1790 }
1791
1792 if (entp == NULL)
1793 break;
1794
1795 if (de.d_name[0] == '.') {
1796 if (de.d_name[1] == '\0' ||
1797 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001798 continue;
1799 }
1800
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001801 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001802 if (err < 0) {
1803 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001804 de.d_name, strerror(-err));
1805 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001806 }
1807
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001808 l = kmod_list_append(list, holder);
1809 if (l != NULL) {
1810 list = l;
1811 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001812 ERR(mod->ctx, "out of memory\n");
1813 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001814 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001815 }
1816 }
1817
1818 closedir(d);
1819 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001820
1821fail:
1822 closedir(d);
1823 kmod_module_unref_list(list);
1824 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001825}
1826
1827struct kmod_module_section {
1828 unsigned long address;
1829 char name[];
1830};
1831
1832static void kmod_module_section_free(struct kmod_module_section *section)
1833{
1834 free(section);
1835}
1836
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001837/**
1838 * kmod_module_get_sections:
1839 * @mod: kmod module
1840 *
1841 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1842 * structure contained in this list is internal to libkmod and their fields
1843 * can be obtained by calling kmod_module_section_get_name() and
1844 * kmod_module_section_get_address().
1845 *
1846 * After use, free the @list by calling kmod_module_section_free_list().
1847 *
1848 * Returns: a new list of kmod module sections on success or NULL on failure.
1849 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001850KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1851{
1852 char dname[PATH_MAX];
1853 struct kmod_list *list = NULL;
1854 DIR *d;
1855 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001856
1857 if (mod == NULL)
1858 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001859
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001860 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1861
1862 d = opendir(dname);
1863 if (d == NULL) {
1864 ERR(mod->ctx, "could not open '%s': %s\n",
1865 dname, strerror(errno));
1866 return NULL;
1867 }
1868
1869 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001870
1871 for (;;) {
1872 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001873 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001874 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001875 unsigned long address;
1876 size_t namesz;
1877 int fd, err;
1878
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001879 err = readdir_r(d, &de, &entp);
1880 if (err != 0) {
1881 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1882 mod->name, strerror(-err));
1883 goto fail;
1884 }
1885
1886 if (de.d_name[0] == '.') {
1887 if (de.d_name[1] == '\0' ||
1888 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001889 continue;
1890 }
1891
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001892 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001893 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001894 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1895 dname, de.d_name);
1896 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001897 }
1898
1899 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001900 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001901 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001902 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1903 dname, de.d_name);
1904 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001905 }
1906
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001907 namesz = strlen(de.d_name) + 1;
1908 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001909
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001910 if (section == NULL) {
1911 ERR(mod->ctx, "out of memory\n");
1912 goto fail;
1913 }
1914
1915 section->address = address;
1916 memcpy(section->name, de.d_name, namesz);
1917
1918 l = kmod_list_append(list, section);
1919 if (l != NULL) {
1920 list = l;
1921 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001922 ERR(mod->ctx, "out of memory\n");
1923 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001924 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001925 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001926 }
1927
1928 closedir(d);
1929 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001930
1931fail:
1932 closedir(d);
1933 kmod_module_unref_list(list);
1934 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001935}
1936
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001937/**
1938 * kmod_module_section_get_module_name:
1939 * @entry: a list entry representing a kmod module section
1940 *
1941 * Get the name of a kmod module section.
1942 *
1943 * After use, free the @list by calling kmod_module_section_free_list().
1944 *
1945 * Returns: the name of this kmod module section on success or NULL on
1946 * failure. The string is owned by the section, do not free it.
1947 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001948KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1949{
1950 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001951
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001952 if (entry == NULL)
1953 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001954
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001955 section = entry->data;
1956 return section->name;
1957}
1958
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001959/**
1960 * kmod_module_section_get_address:
1961 * @entry: a list entry representing a kmod module section
1962 *
1963 * Get the address of a kmod module section.
1964 *
1965 * After use, free the @list by calling kmod_module_section_free_list().
1966 *
1967 * Returns: the address of this kmod module section on success or ULONG_MAX
1968 * on failure.
1969 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001970KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1971{
1972 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001973
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001974 if (entry == NULL)
1975 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001976
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001977 section = entry->data;
1978 return section->address;
1979}
1980
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001981/**
1982 * kmod_module_section_free_list:
1983 * @list: kmod module section list
1984 *
1985 * Release the resources taken by @list
1986 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001987KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1988{
1989 while (list) {
1990 kmod_module_section_free(list->data);
1991 list = kmod_list_remove(list);
1992 }
1993}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001994
1995struct kmod_module_info {
1996 char *key;
1997 char value[];
1998};
1999
2000static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2001{
2002 struct kmod_module_info *info;
2003
2004 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2005 if (info == NULL)
2006 return NULL;
2007
2008 info->key = (char *)info + sizeof(struct kmod_module_info)
2009 + valuelen + 1;
2010 memcpy(info->key, key, keylen);
2011 info->key[keylen] = '\0';
2012 memcpy(info->value, value, valuelen);
2013 info->value[valuelen] = '\0';
2014 return info;
2015}
2016
2017static void kmod_module_info_free(struct kmod_module_info *info)
2018{
2019 free(info);
2020}
2021
2022/**
2023 * kmod_module_get_info:
2024 * @mod: kmod module
2025 * @list: where to return list of module information. Use
2026 * kmod_module_info_get_key() and
2027 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002028 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002029 *
2030 * Get a list of entries in ELF section ".modinfo", these contain
2031 * alias, license, depends, vermagic and other keys with respective
2032 * values.
2033 *
2034 * After use, free the @list by calling kmod_module_info_free_list().
2035 *
2036 * Returns: 0 on success or < 0 otherwise.
2037 */
2038KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2039{
2040 struct kmod_file *file;
2041 struct kmod_elf *elf;
2042 const char *path;
2043 const void *mem;
2044 char **strings;
2045 size_t size;
2046 int i, count, ret = 0;
2047
2048 if (mod == NULL || list == NULL)
2049 return -ENOENT;
2050
2051 assert(*list == NULL);
2052
2053 path = kmod_module_get_path(mod);
2054 if (path == NULL)
2055 return -ENOENT;
2056
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002057 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002058 if (file == NULL)
2059 return -errno;
2060
2061 size = kmod_file_get_size(file);
2062 mem = kmod_file_get_contents(file);
2063
2064 elf = kmod_elf_new(mem, size);
2065 if (elf == NULL) {
2066 ret = -errno;
2067 goto elf_open_error;
2068 }
2069
2070 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
2071 if (count < 0) {
2072 ret = count;
2073 goto get_strings_error;
2074 }
2075
2076 for (i = 0; i < count; i++) {
2077 struct kmod_module_info *info;
2078 struct kmod_list *n;
2079 const char *key, *value;
2080 size_t keylen, valuelen;
2081
2082 key = strings[i];
2083 value = strchr(key, '=');
2084 if (value == NULL) {
2085 keylen = strlen(key);
2086 valuelen = 0;
2087 } else {
2088 keylen = value - key;
2089 value++;
2090 valuelen = strlen(value);
2091 }
2092
2093 info = kmod_module_info_new(key, keylen, value, valuelen);
2094 if (info == NULL) {
2095 ret = -errno;
2096 kmod_module_info_free_list(*list);
2097 *list = NULL;
2098 goto list_error;
2099 }
2100
2101 n = kmod_list_append(*list, info);
2102 if (n != NULL)
2103 *list = n;
2104 else {
2105 kmod_module_info_free(info);
2106 kmod_module_info_free_list(*list);
2107 *list = NULL;
2108 ret = -ENOMEM;
2109 goto list_error;
2110 }
2111 }
2112 ret = count;
2113
2114list_error:
2115 free(strings);
2116get_strings_error:
2117 kmod_elf_unref(elf);
2118elf_open_error:
2119 kmod_file_unref(file);
2120
2121 return ret;
2122}
2123
2124/**
2125 * kmod_module_info_get_key:
2126 * @entry: a list entry representing a kmod module info
2127 *
2128 * Get the key of a kmod module info.
2129 *
2130 * Returns: the key of this kmod module info on success or NULL on
2131 * failure. The string is owned by the info, do not free it.
2132 */
2133KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2134{
2135 struct kmod_module_info *info;
2136
2137 if (entry == NULL)
2138 return NULL;
2139
2140 info = entry->data;
2141 return info->key;
2142}
2143
2144/**
2145 * kmod_module_info_get_value:
2146 * @entry: a list entry representing a kmod module info
2147 *
2148 * Get the value of a kmod module info.
2149 *
2150 * Returns: the value of this kmod module info on success or NULL on
2151 * failure. The string is owned by the info, do not free it.
2152 */
2153KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2154{
2155 struct kmod_module_info *info;
2156
2157 if (entry == NULL)
2158 return NULL;
2159
2160 info = entry->data;
2161 return info->value;
2162}
2163
2164/**
2165 * kmod_module_info_free_list:
2166 * @list: kmod module info list
2167 *
2168 * Release the resources taken by @list
2169 */
2170KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2171{
2172 while (list) {
2173 kmod_module_info_free(list->data);
2174 list = kmod_list_remove(list);
2175 }
2176}
2177
2178struct kmod_module_version {
2179 uint64_t crc;
2180 char symbol[];
2181};
2182
2183static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2184{
2185 struct kmod_module_version *mv;
2186 size_t symbollen = strlen(symbol) + 1;
2187
2188 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2189 if (mv == NULL)
2190 return NULL;
2191
2192 mv->crc = crc;
2193 memcpy(mv->symbol, symbol, symbollen);
2194 return mv;
2195}
2196
2197static void kmod_module_version_free(struct kmod_module_version *version)
2198{
2199 free(version);
2200}
2201
2202/**
2203 * kmod_module_get_versions:
2204 * @mod: kmod module
2205 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002206 * kmod_module_version_get_symbol() and
2207 * kmod_module_version_get_crc(). Release this list with
2208 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002209 *
2210 * Get a list of entries in ELF section "__versions".
2211 *
2212 * After use, free the @list by calling kmod_module_versions_free_list().
2213 *
2214 * Returns: 0 on success or < 0 otherwise.
2215 */
2216KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2217{
2218 struct kmod_file *file;
2219 struct kmod_elf *elf;
2220 const char *path;
2221 const void *mem;
2222 struct kmod_modversion *versions;
2223 size_t size;
2224 int i, count, ret = 0;
2225
2226 if (mod == NULL || list == NULL)
2227 return -ENOENT;
2228
2229 assert(*list == NULL);
2230
2231 path = kmod_module_get_path(mod);
2232 if (path == NULL)
2233 return -ENOENT;
2234
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002235 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002236 if (file == NULL)
2237 return -errno;
2238
2239 size = kmod_file_get_size(file);
2240 mem = kmod_file_get_contents(file);
2241
2242 elf = kmod_elf_new(mem, size);
2243 if (elf == NULL) {
2244 ret = -errno;
2245 goto elf_open_error;
2246 }
2247
2248 count = kmod_elf_get_modversions(elf, &versions);
2249 if (count < 0) {
2250 ret = count;
2251 goto get_strings_error;
2252 }
2253
2254 for (i = 0; i < count; i++) {
2255 struct kmod_module_version *mv;
2256 struct kmod_list *n;
2257
2258 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2259 if (mv == NULL) {
2260 ret = -errno;
2261 kmod_module_versions_free_list(*list);
2262 *list = NULL;
2263 goto list_error;
2264 }
2265
2266 n = kmod_list_append(*list, mv);
2267 if (n != NULL)
2268 *list = n;
2269 else {
2270 kmod_module_version_free(mv);
2271 kmod_module_versions_free_list(*list);
2272 *list = NULL;
2273 ret = -ENOMEM;
2274 goto list_error;
2275 }
2276 }
2277 ret = count;
2278
2279list_error:
2280 free(versions);
2281get_strings_error:
2282 kmod_elf_unref(elf);
2283elf_open_error:
2284 kmod_file_unref(file);
2285
2286 return ret;
2287}
2288
2289/**
2290 * kmod_module_versions_get_symbol:
2291 * @entry: a list entry representing a kmod module versions
2292 *
2293 * Get the symbol of a kmod module versions.
2294 *
2295 * Returns: the symbol of this kmod module versions on success or NULL
2296 * on failure. The string is owned by the versions, do not free it.
2297 */
2298KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2299{
2300 struct kmod_module_version *version;
2301
2302 if (entry == NULL)
2303 return NULL;
2304
2305 version = entry->data;
2306 return version->symbol;
2307}
2308
2309/**
2310 * kmod_module_version_get_crc:
2311 * @entry: a list entry representing a kmod module version
2312 *
2313 * Get the crc of a kmod module version.
2314 *
2315 * Returns: the crc of this kmod module version on success or NULL on
2316 * failure. The string is owned by the version, do not free it.
2317 */
2318KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2319{
2320 struct kmod_module_version *version;
2321
2322 if (entry == NULL)
2323 return 0;
2324
2325 version = entry->data;
2326 return version->crc;
2327}
2328
2329/**
2330 * kmod_module_versions_free_list:
2331 * @list: kmod module versions list
2332 *
2333 * Release the resources taken by @list
2334 */
2335KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2336{
2337 while (list) {
2338 kmod_module_version_free(list->data);
2339 list = kmod_list_remove(list);
2340 }
2341}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002342
2343struct kmod_module_symbol {
2344 uint64_t crc;
2345 char symbol[];
2346};
2347
2348static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2349{
2350 struct kmod_module_symbol *mv;
2351 size_t symbollen = strlen(symbol) + 1;
2352
2353 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2354 if (mv == NULL)
2355 return NULL;
2356
2357 mv->crc = crc;
2358 memcpy(mv->symbol, symbol, symbollen);
2359 return mv;
2360}
2361
2362static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2363{
2364 free(symbol);
2365}
2366
2367/**
2368 * kmod_module_get_symbols:
2369 * @mod: kmod module
2370 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002371 * kmod_module_symbol_get_symbol() and
2372 * kmod_module_symbol_get_crc(). Release this list with
2373 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002374 *
2375 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2376 *
2377 * After use, free the @list by calling kmod_module_symbols_free_list().
2378 *
2379 * Returns: 0 on success or < 0 otherwise.
2380 */
2381KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2382{
2383 struct kmod_file *file;
2384 struct kmod_elf *elf;
2385 const char *path;
2386 const void *mem;
2387 struct kmod_modversion *symbols;
2388 size_t size;
2389 int i, count, ret = 0;
2390
2391 if (mod == NULL || list == NULL)
2392 return -ENOENT;
2393
2394 assert(*list == NULL);
2395
2396 path = kmod_module_get_path(mod);
2397 if (path == NULL)
2398 return -ENOENT;
2399
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002400 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002401 if (file == NULL)
2402 return -errno;
2403
2404 size = kmod_file_get_size(file);
2405 mem = kmod_file_get_contents(file);
2406
2407 elf = kmod_elf_new(mem, size);
2408 if (elf == NULL) {
2409 ret = -errno;
2410 goto elf_open_error;
2411 }
2412
2413 count = kmod_elf_get_symbols(elf, &symbols);
2414 if (count < 0) {
2415 ret = count;
2416 goto get_strings_error;
2417 }
2418
2419 for (i = 0; i < count; i++) {
2420 struct kmod_module_symbol *mv;
2421 struct kmod_list *n;
2422
2423 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2424 if (mv == NULL) {
2425 ret = -errno;
2426 kmod_module_symbols_free_list(*list);
2427 *list = NULL;
2428 goto list_error;
2429 }
2430
2431 n = kmod_list_append(*list, mv);
2432 if (n != NULL)
2433 *list = n;
2434 else {
2435 kmod_module_symbol_free(mv);
2436 kmod_module_symbols_free_list(*list);
2437 *list = NULL;
2438 ret = -ENOMEM;
2439 goto list_error;
2440 }
2441 }
2442 ret = count;
2443
2444list_error:
2445 free(symbols);
2446get_strings_error:
2447 kmod_elf_unref(elf);
2448elf_open_error:
2449 kmod_file_unref(file);
2450
2451 return ret;
2452}
2453
2454/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002455 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002456 * @entry: a list entry representing a kmod module symbols
2457 *
2458 * Get the symbol of a kmod module symbols.
2459 *
2460 * Returns: the symbol of this kmod module symbols on success or NULL
2461 * on failure. The string is owned by the symbols, do not free it.
2462 */
2463KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2464{
2465 struct kmod_module_symbol *symbol;
2466
2467 if (entry == NULL)
2468 return NULL;
2469
2470 symbol = entry->data;
2471 return symbol->symbol;
2472}
2473
2474/**
2475 * kmod_module_symbol_get_crc:
2476 * @entry: a list entry representing a kmod module symbol
2477 *
2478 * Get the crc of a kmod module symbol.
2479 *
2480 * Returns: the crc of this kmod module symbol on success or NULL on
2481 * failure. The string is owned by the symbol, do not free it.
2482 */
2483KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2484{
2485 struct kmod_module_symbol *symbol;
2486
2487 if (entry == NULL)
2488 return 0;
2489
2490 symbol = entry->data;
2491 return symbol->crc;
2492}
2493
2494/**
2495 * kmod_module_symbols_free_list:
2496 * @list: kmod module symbols list
2497 *
2498 * Release the resources taken by @list
2499 */
2500KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2501{
2502 while (list) {
2503 kmod_module_symbol_free(list->data);
2504 list = kmod_list_remove(list);
2505 }
2506}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002507
2508struct kmod_module_dependency_symbol {
2509 uint64_t crc;
2510 uint8_t bind;
2511 char symbol[];
2512};
2513
2514static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2515{
2516 struct kmod_module_dependency_symbol *mv;
2517 size_t symbollen = strlen(symbol) + 1;
2518
2519 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2520 if (mv == NULL)
2521 return NULL;
2522
2523 mv->crc = crc;
2524 mv->bind = bind;
2525 memcpy(mv->symbol, symbol, symbollen);
2526 return mv;
2527}
2528
2529static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2530{
2531 free(dependency_symbol);
2532}
2533
2534/**
2535 * kmod_module_get_dependency_symbols:
2536 * @mod: kmod module
2537 * @list: where to return list of module dependency_symbols. Use
2538 * kmod_module_dependency_symbol_get_symbol() and
2539 * kmod_module_dependency_symbol_get_crc(). Release this list with
2540 * kmod_module_dependency_symbols_free_list()
2541 *
2542 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2543 *
2544 * After use, free the @list by calling
2545 * kmod_module_dependency_symbols_free_list().
2546 *
2547 * Returns: 0 on success or < 0 otherwise.
2548 */
2549KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2550{
2551 struct kmod_file *file;
2552 struct kmod_elf *elf;
2553 const char *path;
2554 const void *mem;
2555 struct kmod_modversion *symbols;
2556 size_t size;
2557 int i, count, ret = 0;
2558
2559 if (mod == NULL || list == NULL)
2560 return -ENOENT;
2561
2562 assert(*list == NULL);
2563
2564 path = kmod_module_get_path(mod);
2565 if (path == NULL)
2566 return -ENOENT;
2567
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002568 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002569 if (file == NULL)
2570 return -errno;
2571
2572 size = kmod_file_get_size(file);
2573 mem = kmod_file_get_contents(file);
2574
2575 elf = kmod_elf_new(mem, size);
2576 if (elf == NULL) {
2577 ret = -errno;
2578 goto elf_open_error;
2579 }
2580
2581 count = kmod_elf_get_dependency_symbols(elf, &symbols);
2582 if (count < 0) {
2583 ret = count;
2584 goto get_strings_error;
2585 }
2586
2587 for (i = 0; i < count; i++) {
2588 struct kmod_module_dependency_symbol *mv;
2589 struct kmod_list *n;
2590
2591 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2592 symbols[i].bind,
2593 symbols[i].symbol);
2594 if (mv == NULL) {
2595 ret = -errno;
2596 kmod_module_dependency_symbols_free_list(*list);
2597 *list = NULL;
2598 goto list_error;
2599 }
2600
2601 n = kmod_list_append(*list, mv);
2602 if (n != NULL)
2603 *list = n;
2604 else {
2605 kmod_module_dependency_symbol_free(mv);
2606 kmod_module_dependency_symbols_free_list(*list);
2607 *list = NULL;
2608 ret = -ENOMEM;
2609 goto list_error;
2610 }
2611 }
2612 ret = count;
2613
2614list_error:
2615 free(symbols);
2616get_strings_error:
2617 kmod_elf_unref(elf);
2618elf_open_error:
2619 kmod_file_unref(file);
2620
2621 return ret;
2622}
2623
2624/**
2625 * kmod_module_dependency_symbol_get_symbol:
2626 * @entry: a list entry representing a kmod module dependency_symbols
2627 *
2628 * Get the dependency symbol of a kmod module
2629 *
2630 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2631 * on failure. The string is owned by the dependency_symbols, do not free it.
2632 */
2633KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2634{
2635 struct kmod_module_dependency_symbol *dependency_symbol;
2636
2637 if (entry == NULL)
2638 return NULL;
2639
2640 dependency_symbol = entry->data;
2641 return dependency_symbol->symbol;
2642}
2643
2644/**
2645 * kmod_module_dependency_symbol_get_crc:
2646 * @entry: a list entry representing a kmod module dependency_symbol
2647 *
2648 * Get the crc of a kmod module dependency_symbol.
2649 *
2650 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2651 * failure. The string is owned by the dependency_symbol, do not free it.
2652 */
2653KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2654{
2655 struct kmod_module_dependency_symbol *dependency_symbol;
2656
2657 if (entry == NULL)
2658 return 0;
2659
2660 dependency_symbol = entry->data;
2661 return dependency_symbol->crc;
2662}
2663
2664/**
2665 * kmod_module_dependency_symbol_get_bind:
2666 * @entry: a list entry representing a kmod module dependency_symbol
2667 *
2668 * Get the bind type of a kmod module dependency_symbol.
2669 *
2670 * Returns: the bind of this kmod module dependency_symbol on success
2671 * or < 0 on failure.
2672 */
2673KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2674{
2675 struct kmod_module_dependency_symbol *dependency_symbol;
2676
2677 if (entry == NULL)
2678 return 0;
2679
2680 dependency_symbol = entry->data;
2681 return dependency_symbol->bind;
2682}
2683
2684/**
2685 * kmod_module_dependency_symbols_free_list:
2686 * @list: kmod module dependency_symbols list
2687 *
2688 * Release the resources taken by @list
2689 */
2690KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2691{
2692 while (list) {
2693 kmod_module_dependency_symbol_free(list->data);
2694 list = kmod_list_remove(list);
2695 }
2696}