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