blob: 4226bbb16b6d74c0aa2652d5f3c84267fe735e7f [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) {
1015 ERR(mod->ctx, "could not get softdep: %s", strerror(-err));
1016 goto fail;
1017 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001018
Lucas De Marchib1a51252012-01-29 01:49:09 -02001019 kmod_list_foreach(l, pre) {
1020 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001021 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001022 if (err < 0)
1023 goto fail;
1024 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001025
Lucas De Marchib1a51252012-01-29 01:49:09 -02001026 l = kmod_list_append(*list, kmod_module_ref(mod));
1027 if (l == NULL) {
1028 kmod_module_unref(mod);
1029 err = -ENOMEM;
1030 goto fail;
1031 }
1032 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001033 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001034
Lucas De Marchib1a51252012-01-29 01:49:09 -02001035 kmod_list_foreach(l, post) {
1036 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001037 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001038 if (err < 0)
1039 goto fail;
1040 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001041
Lucas De Marchib1a51252012-01-29 01:49:09 -02001042fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001043 kmod_module_unref_list(pre);
1044 kmod_module_unref_list(post);
1045
1046 return err;
1047}
1048
Lucas De Marchib1a51252012-01-29 01:49:09 -02001049/* re-entrant */
1050static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001051 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001052 struct kmod_list **list)
1053{
1054 struct kmod_list *dep, *l;
1055 int err = 0;
1056
1057 if (mod->visited) {
1058 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1059 mod->name);
1060 return 0;
1061 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001062 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001063
1064 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001065 kmod_list_foreach(l, dep) {
1066 struct kmod_module *m = l->data;
1067 err = __kmod_module_fill_softdep(m, list);
1068 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001069 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001070 }
1071
Lucas De Marchi89e92482012-01-29 02:35:46 -02001072 if (ignorecmd) {
1073 l = kmod_list_append(*list, kmod_module_ref(mod));
1074 if (l == NULL) {
1075 kmod_module_unref(mod);
1076 err = -ENOMEM;
1077 goto finish;
1078 }
1079 *list = l;
1080 mod->ignorecmd = true;
1081 } else
1082 err = __kmod_module_fill_softdep(mod, list);
1083
Lucas De Marchib1a51252012-01-29 01:49:09 -02001084finish:
1085 kmod_module_unref_list(dep);
1086 return err;
1087}
1088
1089static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001090 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001091 struct kmod_list **list)
1092{
1093 int err;
1094
1095 assert(mod != NULL);
1096 assert(list != NULL && *list == NULL);
1097
1098 /*
1099 * Make sure we don't get screwed by previous calls to this function
1100 */
1101 kmod_set_modules_visited(mod->ctx, false);
1102
Lucas De Marchi89e92482012-01-29 02:35:46 -02001103 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001104 if (err < 0) {
1105 kmod_module_unref_list(*list);
1106 *list = NULL;
1107 }
1108
1109 return err;
1110}
1111
Lucas De Marchiddbda022011-12-27 11:40:10 -02001112/**
1113 * kmod_module_probe_insert_module:
1114 * @mod: kmod module
1115 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1116 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001117 * @extra_options: module's options to pass to Linux Kernel. It applies only
1118 * to @mod, not to its dependencies.
1119 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001120 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001121 * @print_action: function to call with the action being taken (install or
1122 * insmod). It's useful for tools like modprobe when running with verbose
1123 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001124 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001125 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001126 * install commands and applying blacklist.
1127 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001128 * If @run_install is NULL, this function will fork and exec by calling
1129 * system(3). Don't pass a NULL argument in @run_install if your binary is
1130 * setuid/setgid (see warning in system(3)). If you need control over the
1131 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001132 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001133 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1134 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001135 */
1136KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1137 unsigned int flags, const char *extra_options,
1138 int (*run_install)(struct kmod_module *m,
1139 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001140 const void *data,
1141 void (*print_action)(struct kmod_module *m,
1142 bool install,
1143 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001144{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001145 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001146 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001147 int err;
1148
1149 if (mod == NULL)
1150 return -ENOENT;
1151
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001152 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1153 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001154 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001155 return -EEXIST;
1156 else
1157 return 0;
1158 }
1159
Lucas De Marchib1a51252012-01-29 01:49:09 -02001160 err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
1161 KMOD_PROBE_APPLY_BLACKLIST_ALL);
1162 if (err != 0) {
1163 if (module_is_blacklisted(mod))
1164 return err;
1165 }
1166
Lucas De Marchi89e92482012-01-29 02:35:46 -02001167 err = kmod_module_get_probe_list(mod,
1168 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001169 if (err < 0)
1170 return err;
1171
1172 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1173 struct kmod_list *filtered = NULL;
1174
1175 err = kmod_module_get_filtered_blacklist(mod->ctx,
1176 list, &filtered);
1177 if (err < 0)
1178 return err;
1179
1180 kmod_module_unref_list(list);
1181 if (filtered == NULL)
1182 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1183
1184 list = filtered;
1185 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001186
1187 cb.run_install = run_install;
1188 cb.data = (void *) data;
1189
Lucas De Marchib1a51252012-01-29 01:49:09 -02001190 kmod_list_foreach(l, list) {
1191 struct kmod_module *m = l->data;
1192 const char *moptions = kmod_module_get_options(m);
1193 const char *cmd = kmod_module_get_install_commands(m);
1194 char *options = module_options_concat(moptions,
1195 m == mod ? extra_options : NULL);
1196
Lucas De Marchi89e92482012-01-29 02:35:46 -02001197 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001198 if (print_action != NULL)
1199 print_action(m, true, options ?: "");
1200
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001201 if (!(flags & KMOD_PROBE_DRY_RUN))
1202 err = module_do_install_commands(m, options,
1203 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001204 } else {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001205 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1206 && module_is_inkernel(m)) {
Lucas De Marchi7c10c692012-01-30 18:54:45 -02001207 DBG(mod->ctx, "Ignoring module '%s': "
1208 "already loaded\n", m->name);
Lucas De Marchi08600ee2012-02-06 12:52:27 -02001209 err = -EEXIST;
1210 goto finish_module;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001211 }
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001212 if (print_action != NULL)
1213 print_action(m, false, options ?: "");
1214
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001215 if (!(flags & KMOD_PROBE_DRY_RUN))
1216 err = kmod_module_insert_module(m, flags,
1217 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001218 }
1219
Lucas De Marchi08600ee2012-02-06 12:52:27 -02001220finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001221 free(options);
1222
1223 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001224 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001225 * already loaded and the module being loaded is not a softdep
1226 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001227 *
1228 * We need to check here because of race conditions. We
1229 * checked first if module was already loaded but it may have
1230 * been loaded between the check and the moment we try to
1231 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001232 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001233 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001234 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001235 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001236
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001237 if (err == -EEXIST)
1238 err = 0;
1239 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001240 break;
1241 }
1242
1243 kmod_module_unref_list(list);
1244 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001245}
1246
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001247/**
1248 * kmod_module_get_options:
1249 * @mod: kmod module
1250 *
1251 * Get options of this kmod module. Options come from the configuration file
1252 * and are cached in @mod. The first call to this function will search for
1253 * this module in configuration and subsequent calls return the cached string.
1254 *
1255 * Returns: a string with all the options separated by spaces. This string is
1256 * owned by @mod, do not free it.
1257 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001258KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1259{
1260 if (mod == NULL)
1261 return NULL;
1262
1263 if (!mod->init.options) {
1264 /* lazy init */
1265 struct kmod_module *m = (struct kmod_module *)mod;
1266 const struct kmod_list *l, *ctx_options;
1267 char *opts = NULL;
1268 size_t optslen = 0;
1269
1270 ctx_options = kmod_get_options(mod->ctx);
1271
1272 kmod_list_foreach(l, ctx_options) {
1273 const char *modname = kmod_option_get_modname(l);
1274 const char *str;
1275 size_t len;
1276 void *tmp;
1277
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001278 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1279 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1280 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001281 continue;
1282
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001283 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 -02001284 str = kmod_option_get_options(l);
1285 len = strlen(str);
1286 if (len < 1)
1287 continue;
1288
1289 tmp = realloc(opts, optslen + len + 2);
1290 if (tmp == NULL) {
1291 free(opts);
1292 goto failed;
1293 }
1294
1295 opts = tmp;
1296
1297 if (optslen > 0) {
1298 opts[optslen] = ' ';
1299 optslen++;
1300 }
1301
1302 memcpy(opts + optslen, str, len);
1303 optslen += len;
1304 opts[optslen] = '\0';
1305 }
1306
1307 m->init.options = true;
1308 m->options = opts;
1309 }
1310
1311 return mod->options;
1312
1313failed:
1314 ERR(mod->ctx, "out of memory\n");
1315 return NULL;
1316}
1317
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001318/**
1319 * kmod_module_get_install_commands:
1320 * @mod: kmod module
1321 *
1322 * Get install commands for this kmod module. Install commands come from the
1323 * configuration file and are cached in @mod. The first call to this function
1324 * will search for this module in configuration and subsequent calls return
1325 * the cached string. The install commands are returned as they were in the
1326 * configuration, concatenated by ';'. No other processing is made in this
1327 * string.
1328 *
1329 * Returns: a string with all install commands separated by semicolons. This
1330 * string is owned by @mod, do not free it.
1331 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001332KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1333{
1334 if (mod == NULL)
1335 return NULL;
1336
1337 if (!mod->init.install_commands) {
1338 /* lazy init */
1339 struct kmod_module *m = (struct kmod_module *)mod;
1340 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001341
1342 ctx_install_commands = kmod_get_install_commands(mod->ctx);
1343
1344 kmod_list_foreach(l, ctx_install_commands) {
1345 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001346
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001347 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001348 continue;
1349
Lucas De Marchi60f67602011-12-16 03:33:26 -02001350 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001351
Lucas De Marchi60f67602011-12-16 03:33:26 -02001352 /*
1353 * find only the first command, as modprobe from
1354 * module-init-tools does
1355 */
1356 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001357 }
1358
1359 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001360 }
1361
1362 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001363}
1364
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001365void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1366{
1367 mod->init.install_commands = true;
1368 mod->install_commands = cmd;
1369}
1370
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001371static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1372{
1373 struct kmod_list *ret = NULL;
1374 unsigned i;
1375
1376 for (i = 0; i < count; i++) {
1377 const char *depname = array[i];
1378 struct kmod_list *lst = NULL;
1379 int err;
1380
1381 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1382 if (err < 0) {
1383 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1384 continue;
1385 } else if (lst != NULL)
1386 ret = kmod_list_append_list(ret, lst);
1387 }
1388 return ret;
1389}
1390
1391/**
1392 * kmod_module_get_softdeps:
1393 * @mod: kmod module
1394 * @pre: where to save the list of preceding soft dependencies.
1395 * @post: where to save the list of post soft dependencies.
1396 *
1397 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001398 * from configuration file and are not cached in @mod because it may include
1399 * dependency cycles that would make we leak kmod_module. Any call
1400 * to this function will search for this module in configuration, allocate a
1401 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001402 *
1403 * Both @pre and @post are newly created list of kmod_module and
1404 * should be unreferenced with kmod_module_unref_list().
1405 *
1406 * Returns: 0 on success or < 0 otherwise.
1407 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001408KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1409 struct kmod_list **pre,
1410 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001411{
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001412 const struct kmod_list *l, *ctx_softdeps;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001413
1414 if (mod == NULL || pre == NULL || post == NULL)
1415 return -ENOENT;
1416
1417 assert(*pre == NULL);
1418 assert(*post == NULL);
1419
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001420 ctx_softdeps = kmod_get_softdeps(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001421
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001422 kmod_list_foreach(l, ctx_softdeps) {
1423 const char *modname = kmod_softdep_get_name(l);
1424 const char * const *array;
1425 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001426
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001427 if (fnmatch(modname, mod->name, 0) != 0)
1428 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001429
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001430 array = kmod_softdep_get_pre(l, &count);
1431 *pre = lookup_softdep(mod->ctx, array, count);
1432 array = kmod_softdep_get_post(l, &count);
1433 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001434
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001435 /*
1436 * find only the first command, as modprobe from
1437 * module-init-tools does
1438 */
1439 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001440 }
1441
1442 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001443}
1444
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001445/**
1446 * kmod_module_get_remove_commands:
1447 * @mod: kmod module
1448 *
1449 * Get remove commands for this kmod module. Remove commands come from the
1450 * configuration file and are cached in @mod. The first call to this function
1451 * will search for this module in configuration and subsequent calls return
1452 * the cached string. The remove commands are returned as they were in the
1453 * configuration, concatenated by ';'. No other processing is made in this
1454 * string.
1455 *
1456 * Returns: a string with all remove commands separated by semicolons. This
1457 * string is owned by @mod, do not free it.
1458 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001459KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1460{
1461 if (mod == NULL)
1462 return NULL;
1463
1464 if (!mod->init.remove_commands) {
1465 /* lazy init */
1466 struct kmod_module *m = (struct kmod_module *)mod;
1467 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001468
1469 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1470
1471 kmod_list_foreach(l, ctx_remove_commands) {
1472 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001473
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001474 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001475 continue;
1476
Lucas De Marchi60f67602011-12-16 03:33:26 -02001477 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001478
Lucas De Marchi60f67602011-12-16 03:33:26 -02001479 /*
1480 * find only the first command, as modprobe from
1481 * module-init-tools does
1482 */
1483 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001484 }
1485
1486 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001487 }
1488
1489 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001490}
1491
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001492void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1493{
1494 mod->init.remove_commands = true;
1495 mod->remove_commands = cmd;
1496}
1497
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001498/**
1499 * SECTION:libkmod-loaded
1500 * @short_description: currently loaded modules
1501 *
1502 * Information about currently loaded modules, as reported by Linux kernel.
1503 * These information are not cached by libkmod and are always read from /sys
1504 * and /proc/modules.
1505 */
1506
1507/**
1508 * kmod_module_new_from_loaded:
1509 * @ctx: kmod library context
1510 * @list: where to save the list of loaded modules
1511 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001512 * Create a new list of kmod modules with all modules currently loaded in
1513 * kernel. It uses /proc/modules to get the names of loaded modules and to
1514 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1515 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001516 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001517 * The initial refcount is 1, and needs to be decremented to release the
1518 * resources of the kmod_module. The returned @list must be released by
1519 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1520 * kmod_modules created, they are all released upon @ctx destruction too. Do
1521 * not unref @ctx before all the desired operations with the returned list are
1522 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001523 *
1524 * Returns: 0 on success or < 0 on error.
1525 */
1526KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1527 struct kmod_list **list)
1528{
1529 struct kmod_list *l = NULL;
1530 FILE *fp;
1531 char line[4096];
1532
1533 if (ctx == NULL || list == NULL)
1534 return -ENOENT;
1535
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001536 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001537 if (fp == NULL) {
1538 int err = -errno;
1539 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1540 return err;
1541 }
1542
1543 while (fgets(line, sizeof(line), fp)) {
1544 struct kmod_module *m;
1545 struct kmod_list *node;
1546 int err;
1547 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1548
1549 err = kmod_module_new_from_name(ctx, name, &m);
1550 if (err < 0) {
1551 ERR(ctx, "could not get module from name '%s': %s\n",
1552 name, strerror(-err));
1553 continue;
1554 }
1555
1556 node = kmod_list_append(l, m);
1557 if (node)
1558 l = node;
1559 else {
1560 ERR(ctx, "out of memory\n");
1561 kmod_module_unref(m);
1562 }
1563 }
1564
1565 fclose(fp);
1566 *list = l;
1567
1568 return 0;
1569}
1570
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001571/**
1572 * kmod_module_initstate_str:
1573 * @state: the state as returned by kmod_module_get_initstate()
1574 *
1575 * Translate a initstate to a string.
1576 *
1577 * Returns: the string associated to the @state. This string is statically
1578 * allocated, do not free it.
1579 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001580KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1581{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001582 switch (state) {
1583 case KMOD_MODULE_BUILTIN:
1584 return "builtin";
1585 case KMOD_MODULE_LIVE:
1586 return "live";
1587 case KMOD_MODULE_COMING:
1588 return "coming";
1589 case KMOD_MODULE_GOING:
1590 return "going";
1591 default:
1592 return NULL;
1593 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001594}
1595
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001596/**
1597 * kmod_module_get_initstate:
1598 * @mod: kmod module
1599 *
1600 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1601 * /sys filesystem.
1602 *
1603 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1604 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001605KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1606{
1607 char path[PATH_MAX], buf[32];
1608 int fd, err, pathlen;
1609
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001610 if (mod == NULL)
1611 return -ENOENT;
1612
Lucas De Marchi38052742012-02-16 20:43:16 -02001613 if (mod->builtin)
1614 return KMOD_MODULE_BUILTIN;
1615
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001616 pathlen = snprintf(path, sizeof(path),
1617 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001618 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001619 if (fd < 0) {
1620 err = -errno;
1621
Lucas De Marchiddbda022011-12-27 11:40:10 -02001622 DBG(mod->ctx, "could not open '%s': %s\n",
1623 path, strerror(-err));
1624
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001625 if (pathlen > (int)sizeof("/initstate") - 1) {
1626 struct stat st;
1627 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1628 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1629 return KMOD_MODULE_BUILTIN;
1630 }
1631
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001632 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001633 path, strerror(-err));
1634 return err;
1635 }
1636
1637 err = read_str_safe(fd, buf, sizeof(buf));
1638 close(fd);
1639 if (err < 0) {
1640 ERR(mod->ctx, "could not read from '%s': %s\n",
1641 path, strerror(-err));
1642 return err;
1643 }
1644
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001645 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001646 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001647 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001648 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001649 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001650 return KMOD_MODULE_GOING;
1651
1652 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1653 return -EINVAL;
1654}
1655
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001656/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001657 * kmod_module_get_size:
1658 * @mod: kmod module
1659 *
1660 * Get the size of this kmod module as returned by Linux kernel. It reads the
1661 * file /proc/modules to search for this module and get its size.
1662 *
1663 * Returns: the size of this kmod module.
1664 */
1665KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1666{
1667 // FIXME TODO: this should be available from /sys/module/foo
1668 FILE *fp;
1669 char line[4096];
1670 int lineno = 0;
1671 long size = -ENOENT;
1672
1673 if (mod == NULL)
1674 return -ENOENT;
1675
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001676 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001677 if (fp == NULL) {
1678 int err = -errno;
1679 ERR(mod->ctx,
1680 "could not open /proc/modules: %s\n", strerror(errno));
1681 return err;
1682 }
1683
1684 while (fgets(line, sizeof(line), fp)) {
1685 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1686 long value;
1687
1688 lineno++;
1689 if (tok == NULL || !streq(tok, mod->name))
1690 continue;
1691
1692 tok = strtok_r(NULL, " \t", &saveptr);
1693 if (tok == NULL) {
1694 ERR(mod->ctx,
1695 "invalid line format at /proc/modules:%d\n", lineno);
1696 break;
1697 }
1698
1699 value = strtol(tok, &endptr, 10);
1700 if (endptr == tok || *endptr != '\0') {
1701 ERR(mod->ctx,
1702 "invalid line format at /proc/modules:%d\n", lineno);
1703 break;
1704 }
1705
1706 size = value;
1707 break;
1708 }
1709 fclose(fp);
1710 return size;
1711}
1712
1713/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001714 * kmod_module_get_refcnt:
1715 * @mod: kmod module
1716 *
1717 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1718 * /sys filesystem.
1719 *
1720 * Returns: 0 on success or < 0 on failure.
1721 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001722KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1723{
1724 char path[PATH_MAX];
1725 long refcnt;
1726 int fd, err;
1727
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001728 if (mod == NULL)
1729 return -ENOENT;
1730
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001731 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001732 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001733 if (fd < 0) {
1734 err = -errno;
1735 ERR(mod->ctx, "could not open '%s': %s\n",
1736 path, strerror(errno));
1737 return err;
1738 }
1739
1740 err = read_str_long(fd, &refcnt, 10);
1741 close(fd);
1742 if (err < 0) {
1743 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1744 path, strerror(-err));
1745 return err;
1746 }
1747
1748 return (int)refcnt;
1749}
1750
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001751/**
1752 * kmod_module_get_holders:
1753 * @mod: kmod module
1754 *
1755 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1756 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1757 *
1758 * Returns: a new list of kmod modules on success or NULL on failure.
1759 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001760KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1761{
1762 char dname[PATH_MAX];
1763 struct kmod_list *list = NULL;
1764 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001765
1766 if (mod == NULL)
1767 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001768
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001769 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1770
1771 d = opendir(dname);
1772 if (d == NULL) {
1773 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001774 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001775 return NULL;
1776 }
1777
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001778 for (;;) {
1779 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001780 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001781 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001782 int err;
1783
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001784 err = readdir_r(d, &de, &entp);
1785 if (err != 0) {
1786 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1787 mod->name, strerror(-err));
1788 goto fail;
1789 }
1790
1791 if (entp == NULL)
1792 break;
1793
1794 if (de.d_name[0] == '.') {
1795 if (de.d_name[1] == '\0' ||
1796 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001797 continue;
1798 }
1799
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001800 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001801 if (err < 0) {
1802 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001803 de.d_name, strerror(-err));
1804 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001805 }
1806
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001807 l = kmod_list_append(list, holder);
1808 if (l != NULL) {
1809 list = l;
1810 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001811 ERR(mod->ctx, "out of memory\n");
1812 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001813 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001814 }
1815 }
1816
1817 closedir(d);
1818 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001819
1820fail:
1821 closedir(d);
1822 kmod_module_unref_list(list);
1823 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001824}
1825
1826struct kmod_module_section {
1827 unsigned long address;
1828 char name[];
1829};
1830
1831static void kmod_module_section_free(struct kmod_module_section *section)
1832{
1833 free(section);
1834}
1835
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001836/**
1837 * kmod_module_get_sections:
1838 * @mod: kmod module
1839 *
1840 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1841 * structure contained in this list is internal to libkmod and their fields
1842 * can be obtained by calling kmod_module_section_get_name() and
1843 * kmod_module_section_get_address().
1844 *
1845 * After use, free the @list by calling kmod_module_section_free_list().
1846 *
1847 * Returns: a new list of kmod module sections on success or NULL on failure.
1848 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001849KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1850{
1851 char dname[PATH_MAX];
1852 struct kmod_list *list = NULL;
1853 DIR *d;
1854 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001855
1856 if (mod == NULL)
1857 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001858
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001859 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1860
1861 d = opendir(dname);
1862 if (d == NULL) {
1863 ERR(mod->ctx, "could not open '%s': %s\n",
1864 dname, strerror(errno));
1865 return NULL;
1866 }
1867
1868 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001869
1870 for (;;) {
1871 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001872 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001873 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001874 unsigned long address;
1875 size_t namesz;
1876 int fd, err;
1877
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001878 err = readdir_r(d, &de, &entp);
1879 if (err != 0) {
1880 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1881 mod->name, strerror(-err));
1882 goto fail;
1883 }
1884
1885 if (de.d_name[0] == '.') {
1886 if (de.d_name[1] == '\0' ||
1887 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001888 continue;
1889 }
1890
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001891 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001892 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001893 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1894 dname, de.d_name);
1895 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001896 }
1897
1898 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001899 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001900 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001901 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1902 dname, de.d_name);
1903 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001904 }
1905
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001906 namesz = strlen(de.d_name) + 1;
1907 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001908
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001909 if (section == NULL) {
1910 ERR(mod->ctx, "out of memory\n");
1911 goto fail;
1912 }
1913
1914 section->address = address;
1915 memcpy(section->name, de.d_name, namesz);
1916
1917 l = kmod_list_append(list, section);
1918 if (l != NULL) {
1919 list = l;
1920 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001921 ERR(mod->ctx, "out of memory\n");
1922 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001923 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001924 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001925 }
1926
1927 closedir(d);
1928 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001929
1930fail:
1931 closedir(d);
1932 kmod_module_unref_list(list);
1933 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001934}
1935
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001936/**
1937 * kmod_module_section_get_module_name:
1938 * @entry: a list entry representing a kmod module section
1939 *
1940 * Get the name of a kmod module section.
1941 *
1942 * After use, free the @list by calling kmod_module_section_free_list().
1943 *
1944 * Returns: the name of this kmod module section on success or NULL on
1945 * failure. The string is owned by the section, do not free it.
1946 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001947KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1948{
1949 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001950
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001951 if (entry == NULL)
1952 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001953
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001954 section = entry->data;
1955 return section->name;
1956}
1957
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001958/**
1959 * kmod_module_section_get_address:
1960 * @entry: a list entry representing a kmod module section
1961 *
1962 * Get the address of a kmod module section.
1963 *
1964 * After use, free the @list by calling kmod_module_section_free_list().
1965 *
1966 * Returns: the address of this kmod module section on success or ULONG_MAX
1967 * on failure.
1968 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001969KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1970{
1971 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001972
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001973 if (entry == NULL)
1974 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001975
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001976 section = entry->data;
1977 return section->address;
1978}
1979
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001980/**
1981 * kmod_module_section_free_list:
1982 * @list: kmod module section list
1983 *
1984 * Release the resources taken by @list
1985 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001986KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1987{
1988 while (list) {
1989 kmod_module_section_free(list->data);
1990 list = kmod_list_remove(list);
1991 }
1992}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001993
1994struct kmod_module_info {
1995 char *key;
1996 char value[];
1997};
1998
1999static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2000{
2001 struct kmod_module_info *info;
2002
2003 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2004 if (info == NULL)
2005 return NULL;
2006
2007 info->key = (char *)info + sizeof(struct kmod_module_info)
2008 + valuelen + 1;
2009 memcpy(info->key, key, keylen);
2010 info->key[keylen] = '\0';
2011 memcpy(info->value, value, valuelen);
2012 info->value[valuelen] = '\0';
2013 return info;
2014}
2015
2016static void kmod_module_info_free(struct kmod_module_info *info)
2017{
2018 free(info);
2019}
2020
2021/**
2022 * kmod_module_get_info:
2023 * @mod: kmod module
2024 * @list: where to return list of module information. Use
2025 * kmod_module_info_get_key() and
2026 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002027 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002028 *
2029 * Get a list of entries in ELF section ".modinfo", these contain
2030 * alias, license, depends, vermagic and other keys with respective
2031 * values.
2032 *
2033 * After use, free the @list by calling kmod_module_info_free_list().
2034 *
2035 * Returns: 0 on success or < 0 otherwise.
2036 */
2037KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2038{
2039 struct kmod_file *file;
2040 struct kmod_elf *elf;
2041 const char *path;
2042 const void *mem;
2043 char **strings;
2044 size_t size;
2045 int i, count, ret = 0;
2046
2047 if (mod == NULL || list == NULL)
2048 return -ENOENT;
2049
2050 assert(*list == NULL);
2051
2052 path = kmod_module_get_path(mod);
2053 if (path == NULL)
2054 return -ENOENT;
2055
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002056 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002057 if (file == NULL)
2058 return -errno;
2059
2060 size = kmod_file_get_size(file);
2061 mem = kmod_file_get_contents(file);
2062
2063 elf = kmod_elf_new(mem, size);
2064 if (elf == NULL) {
2065 ret = -errno;
2066 goto elf_open_error;
2067 }
2068
2069 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
2070 if (count < 0) {
2071 ret = count;
2072 goto get_strings_error;
2073 }
2074
2075 for (i = 0; i < count; i++) {
2076 struct kmod_module_info *info;
2077 struct kmod_list *n;
2078 const char *key, *value;
2079 size_t keylen, valuelen;
2080
2081 key = strings[i];
2082 value = strchr(key, '=');
2083 if (value == NULL) {
2084 keylen = strlen(key);
2085 valuelen = 0;
2086 } else {
2087 keylen = value - key;
2088 value++;
2089 valuelen = strlen(value);
2090 }
2091
2092 info = kmod_module_info_new(key, keylen, value, valuelen);
2093 if (info == NULL) {
2094 ret = -errno;
2095 kmod_module_info_free_list(*list);
2096 *list = NULL;
2097 goto list_error;
2098 }
2099
2100 n = kmod_list_append(*list, info);
2101 if (n != NULL)
2102 *list = n;
2103 else {
2104 kmod_module_info_free(info);
2105 kmod_module_info_free_list(*list);
2106 *list = NULL;
2107 ret = -ENOMEM;
2108 goto list_error;
2109 }
2110 }
2111 ret = count;
2112
2113list_error:
2114 free(strings);
2115get_strings_error:
2116 kmod_elf_unref(elf);
2117elf_open_error:
2118 kmod_file_unref(file);
2119
2120 return ret;
2121}
2122
2123/**
2124 * kmod_module_info_get_key:
2125 * @entry: a list entry representing a kmod module info
2126 *
2127 * Get the key of a kmod module info.
2128 *
2129 * Returns: the key of this kmod module info on success or NULL on
2130 * failure. The string is owned by the info, do not free it.
2131 */
2132KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2133{
2134 struct kmod_module_info *info;
2135
2136 if (entry == NULL)
2137 return NULL;
2138
2139 info = entry->data;
2140 return info->key;
2141}
2142
2143/**
2144 * kmod_module_info_get_value:
2145 * @entry: a list entry representing a kmod module info
2146 *
2147 * Get the value of a kmod module info.
2148 *
2149 * Returns: the value of this kmod module info on success or NULL on
2150 * failure. The string is owned by the info, do not free it.
2151 */
2152KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2153{
2154 struct kmod_module_info *info;
2155
2156 if (entry == NULL)
2157 return NULL;
2158
2159 info = entry->data;
2160 return info->value;
2161}
2162
2163/**
2164 * kmod_module_info_free_list:
2165 * @list: kmod module info list
2166 *
2167 * Release the resources taken by @list
2168 */
2169KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2170{
2171 while (list) {
2172 kmod_module_info_free(list->data);
2173 list = kmod_list_remove(list);
2174 }
2175}
2176
2177struct kmod_module_version {
2178 uint64_t crc;
2179 char symbol[];
2180};
2181
2182static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2183{
2184 struct kmod_module_version *mv;
2185 size_t symbollen = strlen(symbol) + 1;
2186
2187 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2188 if (mv == NULL)
2189 return NULL;
2190
2191 mv->crc = crc;
2192 memcpy(mv->symbol, symbol, symbollen);
2193 return mv;
2194}
2195
2196static void kmod_module_version_free(struct kmod_module_version *version)
2197{
2198 free(version);
2199}
2200
2201/**
2202 * kmod_module_get_versions:
2203 * @mod: kmod module
2204 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002205 * kmod_module_version_get_symbol() and
2206 * kmod_module_version_get_crc(). Release this list with
2207 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002208 *
2209 * Get a list of entries in ELF section "__versions".
2210 *
2211 * After use, free the @list by calling kmod_module_versions_free_list().
2212 *
2213 * Returns: 0 on success or < 0 otherwise.
2214 */
2215KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2216{
2217 struct kmod_file *file;
2218 struct kmod_elf *elf;
2219 const char *path;
2220 const void *mem;
2221 struct kmod_modversion *versions;
2222 size_t size;
2223 int i, count, ret = 0;
2224
2225 if (mod == NULL || list == NULL)
2226 return -ENOENT;
2227
2228 assert(*list == NULL);
2229
2230 path = kmod_module_get_path(mod);
2231 if (path == NULL)
2232 return -ENOENT;
2233
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002234 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002235 if (file == NULL)
2236 return -errno;
2237
2238 size = kmod_file_get_size(file);
2239 mem = kmod_file_get_contents(file);
2240
2241 elf = kmod_elf_new(mem, size);
2242 if (elf == NULL) {
2243 ret = -errno;
2244 goto elf_open_error;
2245 }
2246
2247 count = kmod_elf_get_modversions(elf, &versions);
2248 if (count < 0) {
2249 ret = count;
2250 goto get_strings_error;
2251 }
2252
2253 for (i = 0; i < count; i++) {
2254 struct kmod_module_version *mv;
2255 struct kmod_list *n;
2256
2257 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2258 if (mv == NULL) {
2259 ret = -errno;
2260 kmod_module_versions_free_list(*list);
2261 *list = NULL;
2262 goto list_error;
2263 }
2264
2265 n = kmod_list_append(*list, mv);
2266 if (n != NULL)
2267 *list = n;
2268 else {
2269 kmod_module_version_free(mv);
2270 kmod_module_versions_free_list(*list);
2271 *list = NULL;
2272 ret = -ENOMEM;
2273 goto list_error;
2274 }
2275 }
2276 ret = count;
2277
2278list_error:
2279 free(versions);
2280get_strings_error:
2281 kmod_elf_unref(elf);
2282elf_open_error:
2283 kmod_file_unref(file);
2284
2285 return ret;
2286}
2287
2288/**
2289 * kmod_module_versions_get_symbol:
2290 * @entry: a list entry representing a kmod module versions
2291 *
2292 * Get the symbol of a kmod module versions.
2293 *
2294 * Returns: the symbol of this kmod module versions on success or NULL
2295 * on failure. The string is owned by the versions, do not free it.
2296 */
2297KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2298{
2299 struct kmod_module_version *version;
2300
2301 if (entry == NULL)
2302 return NULL;
2303
2304 version = entry->data;
2305 return version->symbol;
2306}
2307
2308/**
2309 * kmod_module_version_get_crc:
2310 * @entry: a list entry representing a kmod module version
2311 *
2312 * Get the crc of a kmod module version.
2313 *
2314 * Returns: the crc of this kmod module version on success or NULL on
2315 * failure. The string is owned by the version, do not free it.
2316 */
2317KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2318{
2319 struct kmod_module_version *version;
2320
2321 if (entry == NULL)
2322 return 0;
2323
2324 version = entry->data;
2325 return version->crc;
2326}
2327
2328/**
2329 * kmod_module_versions_free_list:
2330 * @list: kmod module versions list
2331 *
2332 * Release the resources taken by @list
2333 */
2334KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2335{
2336 while (list) {
2337 kmod_module_version_free(list->data);
2338 list = kmod_list_remove(list);
2339 }
2340}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002341
2342struct kmod_module_symbol {
2343 uint64_t crc;
2344 char symbol[];
2345};
2346
2347static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2348{
2349 struct kmod_module_symbol *mv;
2350 size_t symbollen = strlen(symbol) + 1;
2351
2352 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2353 if (mv == NULL)
2354 return NULL;
2355
2356 mv->crc = crc;
2357 memcpy(mv->symbol, symbol, symbollen);
2358 return mv;
2359}
2360
2361static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2362{
2363 free(symbol);
2364}
2365
2366/**
2367 * kmod_module_get_symbols:
2368 * @mod: kmod module
2369 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002370 * kmod_module_symbol_get_symbol() and
2371 * kmod_module_symbol_get_crc(). Release this list with
2372 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002373 *
2374 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2375 *
2376 * After use, free the @list by calling kmod_module_symbols_free_list().
2377 *
2378 * Returns: 0 on success or < 0 otherwise.
2379 */
2380KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2381{
2382 struct kmod_file *file;
2383 struct kmod_elf *elf;
2384 const char *path;
2385 const void *mem;
2386 struct kmod_modversion *symbols;
2387 size_t size;
2388 int i, count, ret = 0;
2389
2390 if (mod == NULL || list == NULL)
2391 return -ENOENT;
2392
2393 assert(*list == NULL);
2394
2395 path = kmod_module_get_path(mod);
2396 if (path == NULL)
2397 return -ENOENT;
2398
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002399 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002400 if (file == NULL)
2401 return -errno;
2402
2403 size = kmod_file_get_size(file);
2404 mem = kmod_file_get_contents(file);
2405
2406 elf = kmod_elf_new(mem, size);
2407 if (elf == NULL) {
2408 ret = -errno;
2409 goto elf_open_error;
2410 }
2411
2412 count = kmod_elf_get_symbols(elf, &symbols);
2413 if (count < 0) {
2414 ret = count;
2415 goto get_strings_error;
2416 }
2417
2418 for (i = 0; i < count; i++) {
2419 struct kmod_module_symbol *mv;
2420 struct kmod_list *n;
2421
2422 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2423 if (mv == NULL) {
2424 ret = -errno;
2425 kmod_module_symbols_free_list(*list);
2426 *list = NULL;
2427 goto list_error;
2428 }
2429
2430 n = kmod_list_append(*list, mv);
2431 if (n != NULL)
2432 *list = n;
2433 else {
2434 kmod_module_symbol_free(mv);
2435 kmod_module_symbols_free_list(*list);
2436 *list = NULL;
2437 ret = -ENOMEM;
2438 goto list_error;
2439 }
2440 }
2441 ret = count;
2442
2443list_error:
2444 free(symbols);
2445get_strings_error:
2446 kmod_elf_unref(elf);
2447elf_open_error:
2448 kmod_file_unref(file);
2449
2450 return ret;
2451}
2452
2453/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002454 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002455 * @entry: a list entry representing a kmod module symbols
2456 *
2457 * Get the symbol of a kmod module symbols.
2458 *
2459 * Returns: the symbol of this kmod module symbols on success or NULL
2460 * on failure. The string is owned by the symbols, do not free it.
2461 */
2462KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2463{
2464 struct kmod_module_symbol *symbol;
2465
2466 if (entry == NULL)
2467 return NULL;
2468
2469 symbol = entry->data;
2470 return symbol->symbol;
2471}
2472
2473/**
2474 * kmod_module_symbol_get_crc:
2475 * @entry: a list entry representing a kmod module symbol
2476 *
2477 * Get the crc of a kmod module symbol.
2478 *
2479 * Returns: the crc of this kmod module symbol on success or NULL on
2480 * failure. The string is owned by the symbol, do not free it.
2481 */
2482KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2483{
2484 struct kmod_module_symbol *symbol;
2485
2486 if (entry == NULL)
2487 return 0;
2488
2489 symbol = entry->data;
2490 return symbol->crc;
2491}
2492
2493/**
2494 * kmod_module_symbols_free_list:
2495 * @list: kmod module symbols list
2496 *
2497 * Release the resources taken by @list
2498 */
2499KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2500{
2501 while (list) {
2502 kmod_module_symbol_free(list->data);
2503 list = kmod_list_remove(list);
2504 }
2505}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002506
2507struct kmod_module_dependency_symbol {
2508 uint64_t crc;
2509 uint8_t bind;
2510 char symbol[];
2511};
2512
2513static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2514{
2515 struct kmod_module_dependency_symbol *mv;
2516 size_t symbollen = strlen(symbol) + 1;
2517
2518 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2519 if (mv == NULL)
2520 return NULL;
2521
2522 mv->crc = crc;
2523 mv->bind = bind;
2524 memcpy(mv->symbol, symbol, symbollen);
2525 return mv;
2526}
2527
2528static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2529{
2530 free(dependency_symbol);
2531}
2532
2533/**
2534 * kmod_module_get_dependency_symbols:
2535 * @mod: kmod module
2536 * @list: where to return list of module dependency_symbols. Use
2537 * kmod_module_dependency_symbol_get_symbol() and
2538 * kmod_module_dependency_symbol_get_crc(). Release this list with
2539 * kmod_module_dependency_symbols_free_list()
2540 *
2541 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2542 *
2543 * After use, free the @list by calling
2544 * kmod_module_dependency_symbols_free_list().
2545 *
2546 * Returns: 0 on success or < 0 otherwise.
2547 */
2548KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2549{
2550 struct kmod_file *file;
2551 struct kmod_elf *elf;
2552 const char *path;
2553 const void *mem;
2554 struct kmod_modversion *symbols;
2555 size_t size;
2556 int i, count, ret = 0;
2557
2558 if (mod == NULL || list == NULL)
2559 return -ENOENT;
2560
2561 assert(*list == NULL);
2562
2563 path = kmod_module_get_path(mod);
2564 if (path == NULL)
2565 return -ENOENT;
2566
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002567 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002568 if (file == NULL)
2569 return -errno;
2570
2571 size = kmod_file_get_size(file);
2572 mem = kmod_file_get_contents(file);
2573
2574 elf = kmod_elf_new(mem, size);
2575 if (elf == NULL) {
2576 ret = -errno;
2577 goto elf_open_error;
2578 }
2579
2580 count = kmod_elf_get_dependency_symbols(elf, &symbols);
2581 if (count < 0) {
2582 ret = count;
2583 goto get_strings_error;
2584 }
2585
2586 for (i = 0; i < count; i++) {
2587 struct kmod_module_dependency_symbol *mv;
2588 struct kmod_list *n;
2589
2590 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2591 symbols[i].bind,
2592 symbols[i].symbol);
2593 if (mv == NULL) {
2594 ret = -errno;
2595 kmod_module_dependency_symbols_free_list(*list);
2596 *list = NULL;
2597 goto list_error;
2598 }
2599
2600 n = kmod_list_append(*list, mv);
2601 if (n != NULL)
2602 *list = n;
2603 else {
2604 kmod_module_dependency_symbol_free(mv);
2605 kmod_module_dependency_symbols_free_list(*list);
2606 *list = NULL;
2607 ret = -ENOMEM;
2608 goto list_error;
2609 }
2610 }
2611 ret = count;
2612
2613list_error:
2614 free(symbols);
2615get_strings_error:
2616 kmod_elf_unref(elf);
2617elf_open_error:
2618 kmod_file_unref(file);
2619
2620 return ret;
2621}
2622
2623/**
2624 * kmod_module_dependency_symbol_get_symbol:
2625 * @entry: a list entry representing a kmod module dependency_symbols
2626 *
2627 * Get the dependency symbol of a kmod module
2628 *
2629 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2630 * on failure. The string is owned by the dependency_symbols, do not free it.
2631 */
2632KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2633{
2634 struct kmod_module_dependency_symbol *dependency_symbol;
2635
2636 if (entry == NULL)
2637 return NULL;
2638
2639 dependency_symbol = entry->data;
2640 return dependency_symbol->symbol;
2641}
2642
2643/**
2644 * kmod_module_dependency_symbol_get_crc:
2645 * @entry: a list entry representing a kmod module dependency_symbol
2646 *
2647 * Get the crc of a kmod module dependency_symbol.
2648 *
2649 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2650 * failure. The string is owned by the dependency_symbol, do not free it.
2651 */
2652KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2653{
2654 struct kmod_module_dependency_symbol *dependency_symbol;
2655
2656 if (entry == NULL)
2657 return 0;
2658
2659 dependency_symbol = entry->data;
2660 return dependency_symbol->crc;
2661}
2662
2663/**
2664 * kmod_module_dependency_symbol_get_bind:
2665 * @entry: a list entry representing a kmod module dependency_symbol
2666 *
2667 * Get the bind type of a kmod module dependency_symbol.
2668 *
2669 * Returns: the bind of this kmod module dependency_symbol on success
2670 * or < 0 on failure.
2671 */
2672KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2673{
2674 struct kmod_module_dependency_symbol *dependency_symbol;
2675
2676 if (entry == NULL)
2677 return 0;
2678
2679 dependency_symbol = entry->data;
2680 return dependency_symbol->bind;
2681}
2682
2683/**
2684 * kmod_module_dependency_symbols_free_list:
2685 * @list: kmod module dependency_symbols list
2686 *
2687 * Release the resources taken by @list
2688 */
2689KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2690{
2691 while (list) {
2692 kmod_module_dependency_symbol_free(list->data);
2693 list = kmod_list_remove(list);
2694 }
2695}