blob: f320fc1b5f7f024470b59cc97d6a859ea60d4f3a [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 Marchi8f788d52011-11-25 01:22:56 -020083};
84
Lucas De Marchic35347f2011-12-12 10:48:02 -020085static inline const char *path_join(const char *path, size_t prefixlen,
86 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020087{
88 size_t pathlen;
89
90 if (path[0] == '/')
91 return path;
92
93 pathlen = strlen(path);
94 if (prefixlen + pathlen + 1 >= PATH_MAX)
95 return NULL;
96
97 memcpy(buf + prefixlen, path, pathlen + 1);
98 return buf;
99}
100
Lucas De Marchi671d4892011-12-05 20:23:05 -0200101int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200102{
103 struct kmod_ctx *ctx = mod->ctx;
104 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200105 const char *dirname;
106 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200107 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200108 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200109 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200110
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200111 if (mod->init.dep)
112 return mod->n_dep;
113 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200114 mod->init.dep = true;
115
116 p = strchr(line, ':');
117 if (p == NULL)
118 return 0;
119
Lucas De Marchi671d4892011-12-05 20:23:05 -0200120 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200121 dirname = kmod_get_dirname(mod->ctx);
122 dirnamelen = strlen(dirname);
123 if (dirnamelen + 2 >= PATH_MAX)
124 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200125
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200126 memcpy(buf, dirname, dirnamelen);
127 buf[dirnamelen] = '/';
128 dirnamelen++;
129 buf[dirnamelen] = '\0';
130
131 if (mod->path == NULL) {
132 const char *str = path_join(line, dirnamelen, buf);
133 if (str == NULL)
134 return 0;
135 mod->path = strdup(str);
136 if (mod->path == NULL)
137 return 0;
138 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200139
Lucas De Marchi7636e722011-12-01 17:56:03 -0200140 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200141 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
142 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200143 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200144 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200145
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200146 path = path_join(p, dirnamelen, buf);
147 if (path == NULL) {
148 ERR(ctx, "could not join path '%s' and '%s'.\n",
149 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200150 goto fail;
151 }
152
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200153 err = kmod_module_new_from_path(ctx, path, &depmod);
154 if (err < 0) {
155 ERR(ctx, "ctx=%p path=%s error=%s\n",
156 ctx, path, strerror(-err));
157 goto fail;
158 }
159
160 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200161
Lucas De Marchib94a7372011-12-26 20:10:49 -0200162 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200163 n++;
164 }
165
166 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
167
168 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200169 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200170 return n;
171
172fail:
173 kmod_module_unref_list(list);
174 mod->init.dep = false;
175 return err;
176}
177
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200178void kmod_module_set_visited(struct kmod_module *mod, bool visited)
179{
180 mod->visited = visited;
181}
182
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200183/**
184 * kmod_module_new_from_name:
185 * @ctx: kmod library context
186 * @name: name of the module
187 * @mod: where to save the created struct kmod_module
188 *
189 * Create a new struct kmod_module using the module name. @name can not be an
190 * alias, file name or anything else; it must be a module name. There's no
191 * check if the module does exists in the system.
192 *
193 * This function is also used internally by many others that return a new
194 * struct kmod_module or a new list of modules.
195 *
196 * The initial refcount is 1, and needs to be decremented to release the
197 * resources of the kmod_module. Since libkmod keeps track of all
198 * kmod_modules created, they are all released upon @ctx destruction too. Do
199 * not unref @ctx before all the desired operations with the returned
200 * kmod_module are done.
201 *
202 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
203 * module name or if memory allocation failed.
204 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200205KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
206 const char *name,
207 struct kmod_module **mod)
208{
209 struct kmod_module *m;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200210 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200211 char name_norm[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200212 char *namesep;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200213
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200214 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200215 return -ENOENT;
216
Luis Felipe Strano Moraes9efaf2f2011-12-20 08:13:56 -0800217 if (alias_normalize(name, name_norm, &namelen) < 0) {
218 DBG(ctx, "invalid alias: %s\n", name);
219 return -EINVAL;
220 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200221
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200222 m = kmod_pool_get_module(ctx, name_norm);
223 if (m != NULL) {
224 *mod = kmod_module_ref(m);
225 return 0;
226 }
227
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200228 namesep = strchr(name_norm, '/');
229 m = malloc(sizeof(*m) + (namesep == NULL ? 1 : 2) * namelen + 2);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200230 if (m == NULL) {
231 free(m);
232 return -ENOMEM;
233 }
234
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200235 memset(m, 0, sizeof(*m));
236
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200237 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200238 m->name = (char *)m + sizeof(*m);
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200239 memcpy(m->name, name_norm, namelen + 1);
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200240
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200241 if (namesep) {
242 size_t len = namesep - name_norm;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200243
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200244 m->name[len] = '\0';
245 m->alias = m->name + len + 1;
246 m->hashkey = m->name + namelen + 1;
247 memcpy(m->hashkey, name_norm, namelen + 1);
248 } else {
249 m->hashkey = m->name;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200250 }
251
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200252 m->refcount = 1;
253 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200254 *mod = m;
255
256 return 0;
257}
258
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200259int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
260 const char *name, struct kmod_module **mod)
261{
262 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200263 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200264 size_t namelen = strlen(name);
265 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200266
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200267 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200268 return -ENAMETOOLONG;
269
270 memcpy(key, name, namelen);
271 memcpy(key + namelen + 1, alias, aliaslen + 1);
272 key[namelen] = '/';
273
274 err = kmod_module_new_from_name(ctx, key, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200275 if (err < 0)
276 return err;
277
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200278 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200279}
280
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200281/**
282 * kmod_module_new_from_path:
283 * @ctx: kmod library context
284 * @path: path where to find the given module
285 * @mod: where to save the created struct kmod_module
286 *
287 * Create a new struct kmod_module using the module path. @path must be an
288 * existent file with in the filesystem and must be accessible to libkmod.
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 * If @path is relative, it's treated as relative to the current working
297 * directory. Otherwise, give an absolute path.
298 *
299 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
300 * it's not a valid file for a kmod_module or if memory allocation failed.
301 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200302KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
303 const char *path,
304 struct kmod_module **mod)
305{
306 struct kmod_module *m;
307 int err;
308 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200309 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200310 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200311 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200312
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200313 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200314 return -ENOENT;
315
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200316 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200317 if (abspath == NULL) {
318 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200319 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200320 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200321
322 err = stat(abspath, &st);
323 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200324 err = -errno;
325 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200326 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200327 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200328 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200329
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200330 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200331 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200332 free(abspath);
333 return -ENOENT;
334 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200335
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200336 m = kmod_pool_get_module(ctx, name);
337 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200338 if (m->path == NULL)
339 m->path = abspath;
340 else if (streq(m->path, abspath))
341 free(abspath);
342 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200343 ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
344 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200345 free(abspath);
346 return -EEXIST;
347 }
348
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200349 *mod = kmod_module_ref(m);
350 return 0;
351 }
352
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200353 m = malloc(sizeof(*m) + namelen + 1);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200354 if (m == NULL)
355 return -errno;
356
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200357 memset(m, 0, sizeof(*m));
358
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200359 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200360 m->name = (char *)m + sizeof(*m);
Lucas De Marchi9dec2442011-12-18 15:12:19 -0200361 memcpy(m->name, name, namelen + 1);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200362 m->path = abspath;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200363 m->hashkey = m->name;
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200364 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200365
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200366 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200367
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200368 *mod = m;
369
370 return 0;
371}
372
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200373/**
374 * kmod_module_unref:
375 * @mod: kmod module
376 *
377 * Drop a reference of the kmod module. If the refcount reaches zero, its
378 * resources are released.
379 *
380 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
381 * returns the passed @mod with its refcount decremented.
382 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200383KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
384{
385 if (mod == NULL)
386 return NULL;
387
388 if (--mod->refcount > 0)
389 return mod;
390
391 DBG(mod->ctx, "kmod_module %p released\n", mod);
392
Lucas De Marchi4084c172011-12-15 13:43:22 -0200393 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200394 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200395 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200396 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200397 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200398 free(mod);
399 return NULL;
400}
401
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200402/**
403 * kmod_module_ref:
404 * @mod: kmod module
405 *
406 * Take a reference of the kmod module, incrementing its refcount.
407 *
408 * Returns: the passed @module with its refcount incremented.
409 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200410KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
411{
412 if (mod == NULL)
413 return NULL;
414
415 mod->refcount++;
416
417 return mod;
418}
419
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200420#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
421 do { \
422 if ((_err) < 0) \
423 goto _label_err; \
424 if (*(_list) != NULL) \
425 goto finish; \
426 } while (0)
427
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200428/**
429 * kmod_module_new_from_lookup:
430 * @ctx: kmod library context
431 * @given_alias: alias to look for
432 * @list: an empty list where to save the list of modules matching
433 * @given_alias
434 *
435 * Create a new list of kmod modules using an alias or module name and lookup
436 * libkmod's configuration files and indexes in order to find the module.
437 * Once it's found in one of the places, it stops searching and create the
438 * list of modules that is saved in @list.
439 *
440 * The search order is: 1. aliases in configuration file; 2. module names in
441 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
442 * in modules.alias index.
443 *
444 * The initial refcount is 1, and needs to be decremented to release the
445 * resources of the kmod_module. The returned @list must be released by
446 * calling kmod_module_unref_list(). Since libkmod keeps track of all
447 * kmod_modules created, they are all released upon @ctx destruction too. Do
448 * not unref @ctx before all the desired operations with the returned list are
449 * completed.
450 *
451 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
452 * methods failed, which is basically due to memory allocation fail. If module
453 * is not found, it still returns 0, but @list is an empty list.
454 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200455KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200456 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200457 struct kmod_list **list)
458{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200459 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200460 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200461
Lucas De Marchi4308b172011-12-13 10:26:04 -0200462 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200463 return -ENOENT;
464
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200465 if (list == NULL || *list != NULL) {
466 ERR(ctx, "An empty list is needed to create lookup\n");
467 return -ENOSYS;
468 }
469
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200470 if (alias_normalize(given_alias, alias, NULL) < 0) {
471 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200472 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200473 }
474
475 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200476
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200477 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200478 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200479 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200480
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200481 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200482 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
483 CHECK_ERR_AND_FINISH(err, fail, list, finish);
484
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200485 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200486 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
487 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200488
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200489 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200490 err = kmod_lookup_alias_from_commands(ctx, alias, list);
491 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200492
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200493 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200494 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
495 CHECK_ERR_AND_FINISH(err, fail, list, finish);
496
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200497finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200498 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200499 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200500fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200501 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200502 kmod_module_unref_list(*list);
503 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200504 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200505}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200506#undef CHECK_ERR_AND_FINISH
507
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200508/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200509 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200510 * @list: list of kmod modules
511 *
512 * Drop a reference of each kmod module in @list and releases the resources
513 * taken by the list itself.
514 *
515 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
516 * returns the passed @mod with its refcount decremented.
517 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200518KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
519{
520 for (; list != NULL; list = kmod_list_remove(list))
521 kmod_module_unref(list->data);
522
523 return 0;
524}
525
Lucas De Marchi0d467432011-12-31 11:15:52 -0200526/**
527 * kmod_module_get_filtered_blacklist:
528 * @ctx: kmod library context
529 * @input: list of kmod_module to be filtered with blacklist
530 * @output: where to save the new list
531 *
532 * Given a list @input, this function filter it out with config's blacklist
533 * ans save it in @output.
534 *
535 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
536 * list.
537 */
538KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
539 const struct kmod_list *input,
540 struct kmod_list **output)
541{
542 const struct kmod_list *li;
543 const struct kmod_list *blacklist;
544
545 if (ctx == NULL || output == NULL)
546 return -ENOENT;
547
548 *output = NULL;
549 if (input == NULL)
550 return 0;
551
552 blacklist = kmod_get_blacklists(ctx);
553 kmod_list_foreach(li, input) {
554 struct kmod_module *mod = li->data;
555 const struct kmod_list *lb;
556 struct kmod_list *node;
557 bool filtered = false;
558
559 kmod_list_foreach(lb, blacklist) {
560 const char *name = lb->data;
561
Lucas De Marchi4926cb52011-12-31 11:21:52 -0200562 if (streq(name, mod->name)) {
Lucas De Marchi0d467432011-12-31 11:15:52 -0200563 filtered = true;
564 break;
565 }
566 }
567
568 if (filtered)
569 continue;
570
571 node = kmod_list_append(*output, mod);
572 if (node == NULL)
573 goto fail;
574
575 *output = node;
576 kmod_module_ref(mod);
577 }
578
579 return 0;
580
581fail:
582 kmod_module_unref_list(*output);
583 *output = NULL;
584 return -ENOMEM;
585}
586
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200587static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
588{
589 if (!mod->init.dep) {
590 /* lazy init */
591 char *line = kmod_search_moddep(mod->ctx, mod->name);
592
593 if (line == NULL)
594 return NULL;
595
596 kmod_module_parse_depline((struct kmod_module *)mod, line);
597 free(line);
598
599 if (!mod->init.dep)
600 return NULL;
601 }
602
603 return mod->dep;
604}
605
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200606/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200607 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200608 * @mod: kmod module
609 *
610 * Search the modules.dep index to find the dependencies of the given @mod.
611 * The result is cached in @mod, so subsequent calls to this function will
612 * return the already searched list of modules.
613 *
614 * Returns: NULL on failure or if there are any dependencies. Otherwise it
615 * returns a list of kmod modules that can be released by calling
616 * kmod_module_unref_list().
617 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200618KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200619{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200620 struct kmod_list *l, *l_new, *list_new = NULL;
621
622 if (mod == NULL)
623 return NULL;
624
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200625 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200626
627 kmod_list_foreach(l, mod->dep) {
628 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
629 if (l_new == NULL) {
630 kmod_module_unref(l->data);
631 goto fail;
632 }
633
634 list_new = l_new;
635 }
636
637 return list_new;
638
639fail:
640 ERR(mod->ctx, "out of memory\n");
641 kmod_module_unref_list(list_new);
642 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200643}
644
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200645/**
646 * kmod_module_get_module:
647 * @entry: an entry in a list of kmod modules.
648 *
649 * Get the kmod module of this @entry in the list, increasing its refcount.
650 * After it's used, unref it. Since the refcount is incremented upon return,
651 * you still have to call kmod_module_unref_list() to release the list of kmod
652 * modules.
653 *
654 * Returns: NULL on failure or the kmod_module contained in this list entry
655 * with its refcount incremented.
656 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200657KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200658{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200659 if (entry == NULL)
660 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200661
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200662 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200663}
664
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200665/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200666 * kmod_module_get_name:
667 * @mod: kmod module
668 *
669 * Get the name of this kmod module. Name is always available, independently
670 * if it was created by kmod_module_new_from_name() or another function and
671 * it's always normalized (dashes are replaced with underscores).
672 *
673 * Returns: the name of this kmod module.
674 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200675KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200676{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200677 if (mod == NULL)
678 return NULL;
679
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200680 return mod->name;
681}
682
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200683/**
684 * kmod_module_get_path:
685 * @mod: kmod module
686 *
687 * Get the path of this kmod module. If this kmod module was not created by
688 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200689 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200690 *
691 * Returns: the path of this kmod module or NULL if such information is not
692 * available.
693 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200694KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200695{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200696 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200697
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200698 if (mod == NULL)
699 return NULL;
700
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200701 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200702
Lucas De Marchie005fac2011-12-08 10:42:34 -0200703 if (mod->path != NULL)
704 return mod->path;
705 if (mod->init.dep)
706 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200707
Lucas De Marchie005fac2011-12-08 10:42:34 -0200708 /* lazy init */
709 line = kmod_search_moddep(mod->ctx, mod->name);
710 if (line == NULL)
711 return NULL;
712
713 kmod_module_parse_depline((struct kmod_module *) mod, line);
714 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200715
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200716 return mod->path;
717}
718
719
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200720extern long delete_module(const char *name, unsigned int flags);
721
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200722/**
723 * kmod_module_remove_module:
724 * @mod: kmod module
725 * @flags: flags to pass to Linux kernel when removing the module
726 *
727 * Remove a module from Linux kernel.
728 *
729 * Returns: 0 on success or < 0 on failure.
730 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200731KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
732 unsigned int flags)
733{
734 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200735
736 if (mod == NULL)
737 return -ENOENT;
738
739 /* Filter out other flags */
740 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
741
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200742 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200743 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200744 err = -errno;
745 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200746 }
747
Lucas De Marchiba998b92012-01-11 00:08:14 -0200748 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200749}
750
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200751extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200752
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200753/**
754 * kmod_module_insert_module:
755 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200756 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
757 * behavior of this function.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200758 * @options: module's options to pass to Linux Kernel.
759 *
760 * Insert a module in Linux kernel. It opens the file pointed by @mod,
761 * mmap'ing it and passing to kernel.
762 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200763 * Returns: 0 on success or < 0 on failure. If module is already loaded it
764 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200765 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200766KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200767 unsigned int flags,
768 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200769{
770 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200771 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200772 off_t size;
773 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200774 struct kmod_elf *elf = NULL;
775 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200776 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200777
778 if (mod == NULL)
779 return -ENOENT;
780
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200781 path = kmod_module_get_path(mod);
782 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500783 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200784 return -ENOSYS;
785 }
786
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200787 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200788 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200789 err = -errno;
790 return err;
791 }
792
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200793 size = kmod_file_get_size(file);
794 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200795
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200796 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
797 elf = kmod_elf_new(mem, size);
798 if (elf == NULL) {
799 err = -errno;
800 goto elf_failed;
801 }
802
803 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
804 err = kmod_elf_strip_section(elf, "__versions");
805 if (err < 0)
806 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
807 }
808
809 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
810 err = kmod_elf_strip_vermagic(elf);
811 if (err < 0)
812 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
813 }
814
815 mem = kmod_elf_get_memory(elf);
816 }
817
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200818 err = init_module(mem, size, args);
Lucas De Marchibbf59322011-12-30 14:13:33 -0200819 if (err < 0) {
820 err = -errno;
821 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
822 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200823
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200824 if (elf != NULL)
825 kmod_elf_unref(elf);
826elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200827 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200828
829 return err;
830}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200831
Lucas De Marchiddbda022011-12-27 11:40:10 -0200832static bool module_is_blacklisted(struct kmod_module *mod)
833{
834 struct kmod_ctx *ctx = mod->ctx;
835 const struct kmod_list *bl = kmod_get_blacklists(ctx);
836 const struct kmod_list *l;
837
838 kmod_list_foreach(l, bl) {
839 const char *modname = kmod_blacklist_get_modname(l);
840
841 if (streq(modname, mod->name))
842 return true;
843 }
844
845 return false;
846}
847
Lucas De Marchiddbda022011-12-27 11:40:10 -0200848static int command_do(struct kmod_module *mod, const char *type,
849 const char *cmd)
850{
851 const char *modname = kmod_module_get_name(mod);
852 int err;
853
854 DBG(mod->ctx, "%s %s\n", type, cmd);
855
856 setenv("MODPROBE_MODULE", modname, 1);
857 err = system(cmd);
858 unsetenv("MODPROBE_MODULE");
859
860 if (err == -1 || WEXITSTATUS(err)) {
861 ERR(mod->ctx, "Error running %s command for %s\n",
862 type, modname);
863 if (err != -1)
864 err = -WEXITSTATUS(err);
865 }
866
867 return err;
868}
869
Lucas De Marchib1a51252012-01-29 01:49:09 -0200870struct probe_insert_cb {
871 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
872 void *data;
873};
874
Lucas De Marchiddbda022011-12-27 11:40:10 -0200875static int module_do_install_commands(struct kmod_module *mod,
876 const char *options,
877 struct probe_insert_cb *cb)
878{
879 const char *command = kmod_module_get_install_commands(mod);
880 char *p, *cmd;
881 int err;
882 size_t cmdlen, options_len, varlen;
883
884 assert(command);
885
886 if (options == NULL)
887 options = "";
888
889 options_len = strlen(options);
890 cmdlen = strlen(command);
891 varlen = sizeof("$CMDLINE_OPTS") - 1;
892
893 cmd = memdup(command, cmdlen + 1);
894 if (cmd == NULL)
895 return -ENOMEM;
896
897 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
898 size_t prefixlen = p - cmd;
899 size_t suffixlen = cmdlen - prefixlen - varlen;
900 size_t slen = cmdlen - varlen + options_len;
901 char *suffix = p + varlen;
902 char *s = malloc(slen + 1);
903 if (s == NULL) {
904 free(cmd);
905 return -ENOMEM;
906 }
907 memcpy(s, cmd, p - cmd);
908 memcpy(s + prefixlen, options, options_len);
909 memcpy(s + prefixlen + options_len, suffix, suffixlen);
910 s[slen] = '\0';
911
912 free(cmd);
913 cmd = s;
914 cmdlen = slen;
915 }
916
917 if (cb->run_install != NULL)
918 err = cb->run_install(mod, cmd, cb->data);
919 else
920 err = command_do(mod, "install", cmd);
921
922 free(cmd);
923
924 return err;
925}
926
Lucas De Marchiddbda022011-12-27 11:40:10 -0200927static char *module_options_concat(const char *opt, const char *xopt)
928{
929 // TODO: we might need to check if xopt overrides options on opt
930 size_t optlen = opt == NULL ? 0 : strlen(opt);
931 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
932 char *r;
933
934 if (optlen == 0 && xoptlen == 0)
935 return NULL;
936
937 r = malloc(optlen + xoptlen + 2);
938
939 if (opt != NULL) {
940 memcpy(r, opt, optlen);
941 r[optlen] = ' ';
942 optlen++;
943 }
944
945 if (xopt != NULL)
946 memcpy(r + optlen, xopt, xoptlen);
947
948 r[optlen + xoptlen] = '\0';
949
950 return r;
951}
952
Lucas De Marchib1a51252012-01-29 01:49:09 -0200953static int __kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -0200954 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -0200955 struct kmod_list **list);
956
957/* re-entrant */
958static int __kmod_module_fill_softdep(struct kmod_module *mod,
959 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -0200960{
Lucas De Marchib1a51252012-01-29 01:49:09 -0200961 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200962 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200963
964 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -0200965 if (err < 0) {
966 ERR(mod->ctx, "could not get softdep: %s", strerror(-err));
967 goto fail;
968 }
Lucas De Marchiddbda022011-12-27 11:40:10 -0200969
Lucas De Marchib1a51252012-01-29 01:49:09 -0200970 kmod_list_foreach(l, pre) {
971 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -0200972 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -0200973 if (err < 0)
974 goto fail;
975 }
Lucas De Marchiddbda022011-12-27 11:40:10 -0200976
Lucas De Marchib1a51252012-01-29 01:49:09 -0200977 l = kmod_list_append(*list, kmod_module_ref(mod));
978 if (l == NULL) {
979 kmod_module_unref(mod);
980 err = -ENOMEM;
981 goto fail;
982 }
983 *list = l;
984 mod->visited = true;
Lucas De Marchi89e92482012-01-29 02:35:46 -0200985 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -0200986
Lucas De Marchib1a51252012-01-29 01:49:09 -0200987 kmod_list_foreach(l, post) {
988 struct kmod_module *m = l->data;
Lucas De Marchi89e92482012-01-29 02:35:46 -0200989 err = __kmod_module_get_probe_list(m, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -0200990 if (err < 0)
991 goto fail;
992 }
Lucas De Marchiddbda022011-12-27 11:40:10 -0200993
Lucas De Marchib1a51252012-01-29 01:49:09 -0200994fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -0200995 kmod_module_unref_list(pre);
996 kmod_module_unref_list(post);
997
998 return err;
999}
1000
Lucas De Marchib1a51252012-01-29 01:49:09 -02001001/* re-entrant */
1002static 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 struct kmod_list *dep, *l;
1007 int err = 0;
1008
1009 if (mod->visited) {
1010 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1011 mod->name);
1012 return 0;
1013 }
1014
1015 dep = kmod_module_get_dependencies(mod);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001016 kmod_list_foreach(l, dep) {
1017 struct kmod_module *m = l->data;
1018 err = __kmod_module_fill_softdep(m, list);
1019 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001020 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001021 }
1022
Lucas De Marchi89e92482012-01-29 02:35:46 -02001023 if (ignorecmd) {
1024 l = kmod_list_append(*list, kmod_module_ref(mod));
1025 if (l == NULL) {
1026 kmod_module_unref(mod);
1027 err = -ENOMEM;
1028 goto finish;
1029 }
1030 *list = l;
1031 mod->ignorecmd = true;
1032 } else
1033 err = __kmod_module_fill_softdep(mod, list);
1034
Lucas De Marchib1a51252012-01-29 01:49:09 -02001035finish:
1036 kmod_module_unref_list(dep);
1037 return err;
1038}
1039
1040static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001041 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001042 struct kmod_list **list)
1043{
1044 int err;
1045
1046 assert(mod != NULL);
1047 assert(list != NULL && *list == NULL);
1048
1049 /*
1050 * Make sure we don't get screwed by previous calls to this function
1051 */
1052 kmod_set_modules_visited(mod->ctx, false);
1053
Lucas De Marchi89e92482012-01-29 02:35:46 -02001054 err = __kmod_module_get_probe_list(mod, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001055 if (err < 0) {
1056 kmod_module_unref_list(*list);
1057 *list = NULL;
1058 }
1059
1060 return err;
1061}
1062
Lucas De Marchiddbda022011-12-27 11:40:10 -02001063/**
1064 * kmod_module_probe_insert_module:
1065 * @mod: kmod module
1066 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1067 * behavior of this function.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001068 * @extra_options: module's options to pass to Linux Kernel. It applies only
1069 * to @mod, not to its dependencies.
1070 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001071 * @data: data to give back to @run_install callback
1072 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001073 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001074 * install commands and applying blacklist.
1075 *
1076 * If @run_install is NULL, and the flag KMOD_PROBE_STOP_ON_COMMANDS is not
Lucas De Marchib1a51252012-01-29 01:49:09 -02001077 * given, this function will fork and exec by calling system(3). Don't pass a
1078 * NULL argument in @run_install if your binary is setuid/setgid (see warning
1079 * in system(3)). If you need control over the execution of an install
1080 * command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001081 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001082 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1083 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001084 */
1085KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1086 unsigned int flags, const char *extra_options,
1087 int (*run_install)(struct kmod_module *m,
1088 const char *cmd, void *data),
1089 const void *data)
1090{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001091 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001092 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001093 int err;
1094
1095 if (mod == NULL)
1096 return -ENOENT;
1097
1098 err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
1099 KMOD_PROBE_APPLY_BLACKLIST_ALL);
1100 if (err != 0) {
1101 if (module_is_blacklisted(mod))
1102 return err;
1103 }
1104
Lucas De Marchi89e92482012-01-29 02:35:46 -02001105 err = kmod_module_get_probe_list(mod,
1106 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001107 if (err < 0)
1108 return err;
1109
1110 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1111 struct kmod_list *filtered = NULL;
1112
1113 err = kmod_module_get_filtered_blacklist(mod->ctx,
1114 list, &filtered);
1115 if (err < 0)
1116 return err;
1117
1118 kmod_module_unref_list(list);
1119 if (filtered == NULL)
1120 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1121
1122 list = filtered;
1123 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001124
1125 cb.run_install = run_install;
1126 cb.data = (void *) data;
1127
Lucas De Marchib1a51252012-01-29 01:49:09 -02001128 kmod_list_foreach(l, list) {
1129 struct kmod_module *m = l->data;
1130 const char *moptions = kmod_module_get_options(m);
1131 const char *cmd = kmod_module_get_install_commands(m);
1132 char *options = module_options_concat(moptions,
1133 m == mod ? extra_options : NULL);
1134
Lucas De Marchi89e92482012-01-29 02:35:46 -02001135 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001136 if (flags & KMOD_PROBE_STOP_ON_COMMAND) {
1137 DBG(mod->ctx, "Stopping on '%s': "
1138 "install command\n", m->name);
1139 err = KMOD_PROBE_STOP_ON_COMMAND;
1140 free(options);
1141 break;
1142 }
1143 err = module_do_install_commands(m, options, &cb);
1144 } else {
1145 int state = kmod_module_get_initstate(m);
1146
1147 if (state == KMOD_MODULE_LIVE ||
1148 state == KMOD_MODULE_COMING ||
1149 state == KMOD_MODULE_BUILTIN) {
1150 DBG(mod->ctx, "Ignoring '%s': "
1151 "module already loaded\n", m->name);
1152 free(options);
1153 continue;
1154 }
1155 err = kmod_module_insert_module(m, flags, options);
1156 }
1157
1158 free(options);
1159
1160 /*
1161 * Ignore "already loaded" error. We need to check here
1162 * because of race conditions. We checked first if module was
1163 * already loaded but it may have been loaded between the
1164 * check and the moment we try to insert it.
1165 */
1166 if (err < 0 && err != -EEXIST &&
1167 (flags & KMOD_PROBE_STOP_ON_DEP_FAILURE))
1168 break;
1169 }
1170
1171 kmod_module_unref_list(list);
1172 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001173}
1174
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001175/**
1176 * kmod_module_get_options:
1177 * @mod: kmod module
1178 *
1179 * Get options of this kmod module. Options come from the configuration file
1180 * and are cached in @mod. The first call to this function will search for
1181 * this module in configuration and subsequent calls return the cached string.
1182 *
1183 * Returns: a string with all the options separated by spaces. This string is
1184 * owned by @mod, do not free it.
1185 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001186KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1187{
1188 if (mod == NULL)
1189 return NULL;
1190
1191 if (!mod->init.options) {
1192 /* lazy init */
1193 struct kmod_module *m = (struct kmod_module *)mod;
1194 const struct kmod_list *l, *ctx_options;
1195 char *opts = NULL;
1196 size_t optslen = 0;
1197
1198 ctx_options = kmod_get_options(mod->ctx);
1199
1200 kmod_list_foreach(l, ctx_options) {
1201 const char *modname = kmod_option_get_modname(l);
1202 const char *str;
1203 size_t len;
1204 void *tmp;
1205
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001206 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1207 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1208 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001209 continue;
1210
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001211 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 -02001212 str = kmod_option_get_options(l);
1213 len = strlen(str);
1214 if (len < 1)
1215 continue;
1216
1217 tmp = realloc(opts, optslen + len + 2);
1218 if (tmp == NULL) {
1219 free(opts);
1220 goto failed;
1221 }
1222
1223 opts = tmp;
1224
1225 if (optslen > 0) {
1226 opts[optslen] = ' ';
1227 optslen++;
1228 }
1229
1230 memcpy(opts + optslen, str, len);
1231 optslen += len;
1232 opts[optslen] = '\0';
1233 }
1234
1235 m->init.options = true;
1236 m->options = opts;
1237 }
1238
1239 return mod->options;
1240
1241failed:
1242 ERR(mod->ctx, "out of memory\n");
1243 return NULL;
1244}
1245
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001246/**
1247 * kmod_module_get_install_commands:
1248 * @mod: kmod module
1249 *
1250 * Get install commands for this kmod module. Install commands come from the
1251 * configuration file and are cached in @mod. The first call to this function
1252 * will search for this module in configuration and subsequent calls return
1253 * the cached string. The install commands are returned as they were in the
1254 * configuration, concatenated by ';'. No other processing is made in this
1255 * string.
1256 *
1257 * Returns: a string with all install commands separated by semicolons. This
1258 * string is owned by @mod, do not free it.
1259 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001260KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1261{
1262 if (mod == NULL)
1263 return NULL;
1264
1265 if (!mod->init.install_commands) {
1266 /* lazy init */
1267 struct kmod_module *m = (struct kmod_module *)mod;
1268 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001269
1270 ctx_install_commands = kmod_get_install_commands(mod->ctx);
1271
1272 kmod_list_foreach(l, ctx_install_commands) {
1273 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001274
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001275 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001276 continue;
1277
Lucas De Marchi60f67602011-12-16 03:33:26 -02001278 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001279
Lucas De Marchi60f67602011-12-16 03:33:26 -02001280 /*
1281 * find only the first command, as modprobe from
1282 * module-init-tools does
1283 */
1284 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001285 }
1286
1287 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001288 }
1289
1290 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001291}
1292
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001293void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1294{
1295 mod->init.install_commands = true;
1296 mod->install_commands = cmd;
1297}
1298
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001299static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1300{
1301 struct kmod_list *ret = NULL;
1302 unsigned i;
1303
1304 for (i = 0; i < count; i++) {
1305 const char *depname = array[i];
1306 struct kmod_list *lst = NULL;
1307 int err;
1308
1309 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1310 if (err < 0) {
1311 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1312 continue;
1313 } else if (lst != NULL)
1314 ret = kmod_list_append_list(ret, lst);
1315 }
1316 return ret;
1317}
1318
1319/**
1320 * kmod_module_get_softdeps:
1321 * @mod: kmod module
1322 * @pre: where to save the list of preceding soft dependencies.
1323 * @post: where to save the list of post soft dependencies.
1324 *
1325 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001326 * from configuration file and are not cached in @mod because it may include
1327 * dependency cycles that would make we leak kmod_module. Any call
1328 * to this function will search for this module in configuration, allocate a
1329 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001330 *
1331 * Both @pre and @post are newly created list of kmod_module and
1332 * should be unreferenced with kmod_module_unref_list().
1333 *
1334 * Returns: 0 on success or < 0 otherwise.
1335 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001336KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1337 struct kmod_list **pre,
1338 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001339{
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001340 const struct kmod_list *l, *ctx_softdeps;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001341
1342 if (mod == NULL || pre == NULL || post == NULL)
1343 return -ENOENT;
1344
1345 assert(*pre == NULL);
1346 assert(*post == NULL);
1347
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001348 ctx_softdeps = kmod_get_softdeps(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001349
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001350 kmod_list_foreach(l, ctx_softdeps) {
1351 const char *modname = kmod_softdep_get_name(l);
1352 const char * const *array;
1353 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001354
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001355 if (fnmatch(modname, mod->name, 0) != 0)
1356 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001357
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001358 array = kmod_softdep_get_pre(l, &count);
1359 *pre = lookup_softdep(mod->ctx, array, count);
1360 array = kmod_softdep_get_post(l, &count);
1361 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001362
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001363 /*
1364 * find only the first command, as modprobe from
1365 * module-init-tools does
1366 */
1367 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001368 }
1369
1370 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001371}
1372
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001373/**
1374 * kmod_module_get_remove_commands:
1375 * @mod: kmod module
1376 *
1377 * Get remove commands for this kmod module. Remove commands come from the
1378 * configuration file and are cached in @mod. The first call to this function
1379 * will search for this module in configuration and subsequent calls return
1380 * the cached string. The remove commands are returned as they were in the
1381 * configuration, concatenated by ';'. No other processing is made in this
1382 * string.
1383 *
1384 * Returns: a string with all remove commands separated by semicolons. This
1385 * string is owned by @mod, do not free it.
1386 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001387KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1388{
1389 if (mod == NULL)
1390 return NULL;
1391
1392 if (!mod->init.remove_commands) {
1393 /* lazy init */
1394 struct kmod_module *m = (struct kmod_module *)mod;
1395 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001396
1397 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1398
1399 kmod_list_foreach(l, ctx_remove_commands) {
1400 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001401
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001402 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001403 continue;
1404
Lucas De Marchi60f67602011-12-16 03:33:26 -02001405 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001406
Lucas De Marchi60f67602011-12-16 03:33:26 -02001407 /*
1408 * find only the first command, as modprobe from
1409 * module-init-tools does
1410 */
1411 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001412 }
1413
1414 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001415 }
1416
1417 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001418}
1419
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001420void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1421{
1422 mod->init.remove_commands = true;
1423 mod->remove_commands = cmd;
1424}
1425
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001426/**
1427 * SECTION:libkmod-loaded
1428 * @short_description: currently loaded modules
1429 *
1430 * Information about currently loaded modules, as reported by Linux kernel.
1431 * These information are not cached by libkmod and are always read from /sys
1432 * and /proc/modules.
1433 */
1434
1435/**
1436 * kmod_module_new_from_loaded:
1437 * @ctx: kmod library context
1438 * @list: where to save the list of loaded modules
1439 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001440 * Create a new list of kmod modules with all modules currently loaded in
1441 * kernel. It uses /proc/modules to get the names of loaded modules and to
1442 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1443 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001444 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001445 * The initial refcount is 1, and needs to be decremented to release the
1446 * resources of the kmod_module. The returned @list must be released by
1447 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1448 * kmod_modules created, they are all released upon @ctx destruction too. Do
1449 * not unref @ctx before all the desired operations with the returned list are
1450 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001451 *
1452 * Returns: 0 on success or < 0 on error.
1453 */
1454KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1455 struct kmod_list **list)
1456{
1457 struct kmod_list *l = NULL;
1458 FILE *fp;
1459 char line[4096];
1460
1461 if (ctx == NULL || list == NULL)
1462 return -ENOENT;
1463
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001464 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001465 if (fp == NULL) {
1466 int err = -errno;
1467 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1468 return err;
1469 }
1470
1471 while (fgets(line, sizeof(line), fp)) {
1472 struct kmod_module *m;
1473 struct kmod_list *node;
1474 int err;
1475 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1476
1477 err = kmod_module_new_from_name(ctx, name, &m);
1478 if (err < 0) {
1479 ERR(ctx, "could not get module from name '%s': %s\n",
1480 name, strerror(-err));
1481 continue;
1482 }
1483
1484 node = kmod_list_append(l, m);
1485 if (node)
1486 l = node;
1487 else {
1488 ERR(ctx, "out of memory\n");
1489 kmod_module_unref(m);
1490 }
1491 }
1492
1493 fclose(fp);
1494 *list = l;
1495
1496 return 0;
1497}
1498
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001499/**
1500 * kmod_module_initstate_str:
1501 * @state: the state as returned by kmod_module_get_initstate()
1502 *
1503 * Translate a initstate to a string.
1504 *
1505 * Returns: the string associated to the @state. This string is statically
1506 * allocated, do not free it.
1507 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001508KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1509{
1510 switch (state) {
1511 case KMOD_MODULE_BUILTIN:
1512 return "builtin";
1513 case KMOD_MODULE_LIVE:
1514 return "live";
1515 case KMOD_MODULE_COMING:
1516 return "coming";
1517 case KMOD_MODULE_GOING:
1518 return "going";
1519 default:
1520 return NULL;
1521 }
1522}
1523
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001524/**
1525 * kmod_module_get_initstate:
1526 * @mod: kmod module
1527 *
1528 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1529 * /sys filesystem.
1530 *
1531 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1532 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001533KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1534{
1535 char path[PATH_MAX], buf[32];
1536 int fd, err, pathlen;
1537
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001538 if (mod == NULL)
1539 return -ENOENT;
1540
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001541 pathlen = snprintf(path, sizeof(path),
1542 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001543 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001544 if (fd < 0) {
1545 err = -errno;
1546
Lucas De Marchiddbda022011-12-27 11:40:10 -02001547 DBG(mod->ctx, "could not open '%s': %s\n",
1548 path, strerror(-err));
1549
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001550 if (pathlen > (int)sizeof("/initstate") - 1) {
1551 struct stat st;
1552 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1553 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1554 return KMOD_MODULE_BUILTIN;
1555 }
1556
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001557 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001558 path, strerror(-err));
1559 return err;
1560 }
1561
1562 err = read_str_safe(fd, buf, sizeof(buf));
1563 close(fd);
1564 if (err < 0) {
1565 ERR(mod->ctx, "could not read from '%s': %s\n",
1566 path, strerror(-err));
1567 return err;
1568 }
1569
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001570 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001571 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001572 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001573 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001574 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001575 return KMOD_MODULE_GOING;
1576
1577 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1578 return -EINVAL;
1579}
1580
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001581/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001582 * kmod_module_get_size:
1583 * @mod: kmod module
1584 *
1585 * Get the size of this kmod module as returned by Linux kernel. It reads the
1586 * file /proc/modules to search for this module and get its size.
1587 *
1588 * Returns: the size of this kmod module.
1589 */
1590KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1591{
1592 // FIXME TODO: this should be available from /sys/module/foo
1593 FILE *fp;
1594 char line[4096];
1595 int lineno = 0;
1596 long size = -ENOENT;
1597
1598 if (mod == NULL)
1599 return -ENOENT;
1600
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001601 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001602 if (fp == NULL) {
1603 int err = -errno;
1604 ERR(mod->ctx,
1605 "could not open /proc/modules: %s\n", strerror(errno));
1606 return err;
1607 }
1608
1609 while (fgets(line, sizeof(line), fp)) {
1610 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1611 long value;
1612
1613 lineno++;
1614 if (tok == NULL || !streq(tok, mod->name))
1615 continue;
1616
1617 tok = strtok_r(NULL, " \t", &saveptr);
1618 if (tok == NULL) {
1619 ERR(mod->ctx,
1620 "invalid line format at /proc/modules:%d\n", lineno);
1621 break;
1622 }
1623
1624 value = strtol(tok, &endptr, 10);
1625 if (endptr == tok || *endptr != '\0') {
1626 ERR(mod->ctx,
1627 "invalid line format at /proc/modules:%d\n", lineno);
1628 break;
1629 }
1630
1631 size = value;
1632 break;
1633 }
1634 fclose(fp);
1635 return size;
1636}
1637
1638/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001639 * kmod_module_get_refcnt:
1640 * @mod: kmod module
1641 *
1642 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1643 * /sys filesystem.
1644 *
1645 * Returns: 0 on success or < 0 on failure.
1646 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001647KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1648{
1649 char path[PATH_MAX];
1650 long refcnt;
1651 int fd, err;
1652
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001653 if (mod == NULL)
1654 return -ENOENT;
1655
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001656 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001657 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001658 if (fd < 0) {
1659 err = -errno;
1660 ERR(mod->ctx, "could not open '%s': %s\n",
1661 path, strerror(errno));
1662 return err;
1663 }
1664
1665 err = read_str_long(fd, &refcnt, 10);
1666 close(fd);
1667 if (err < 0) {
1668 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1669 path, strerror(-err));
1670 return err;
1671 }
1672
1673 return (int)refcnt;
1674}
1675
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001676/**
1677 * kmod_module_get_holders:
1678 * @mod: kmod module
1679 *
1680 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1681 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1682 *
1683 * Returns: a new list of kmod modules on success or NULL on failure.
1684 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001685KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1686{
1687 char dname[PATH_MAX];
1688 struct kmod_list *list = NULL;
1689 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001690
1691 if (mod == NULL)
1692 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001693
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001694 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1695
1696 d = opendir(dname);
1697 if (d == NULL) {
1698 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001699 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001700 return NULL;
1701 }
1702
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001703 for (;;) {
1704 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001705 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001706 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001707 int err;
1708
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001709 err = readdir_r(d, &de, &entp);
1710 if (err != 0) {
1711 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1712 mod->name, strerror(-err));
1713 goto fail;
1714 }
1715
1716 if (entp == NULL)
1717 break;
1718
1719 if (de.d_name[0] == '.') {
1720 if (de.d_name[1] == '\0' ||
1721 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001722 continue;
1723 }
1724
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001725 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001726 if (err < 0) {
1727 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001728 de.d_name, strerror(-err));
1729 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001730 }
1731
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001732 l = kmod_list_append(list, holder);
1733 if (l != NULL) {
1734 list = l;
1735 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001736 ERR(mod->ctx, "out of memory\n");
1737 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001738 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001739 }
1740 }
1741
1742 closedir(d);
1743 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001744
1745fail:
1746 closedir(d);
1747 kmod_module_unref_list(list);
1748 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001749}
1750
1751struct kmod_module_section {
1752 unsigned long address;
1753 char name[];
1754};
1755
1756static void kmod_module_section_free(struct kmod_module_section *section)
1757{
1758 free(section);
1759}
1760
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001761/**
1762 * kmod_module_get_sections:
1763 * @mod: kmod module
1764 *
1765 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1766 * structure contained in this list is internal to libkmod and their fields
1767 * can be obtained by calling kmod_module_section_get_name() and
1768 * kmod_module_section_get_address().
1769 *
1770 * After use, free the @list by calling kmod_module_section_free_list().
1771 *
1772 * Returns: a new list of kmod module sections on success or NULL on failure.
1773 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001774KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1775{
1776 char dname[PATH_MAX];
1777 struct kmod_list *list = NULL;
1778 DIR *d;
1779 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001780
1781 if (mod == NULL)
1782 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001783
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001784 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1785
1786 d = opendir(dname);
1787 if (d == NULL) {
1788 ERR(mod->ctx, "could not open '%s': %s\n",
1789 dname, strerror(errno));
1790 return NULL;
1791 }
1792
1793 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001794
1795 for (;;) {
1796 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001797 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001798 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001799 unsigned long address;
1800 size_t namesz;
1801 int fd, err;
1802
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001803 err = readdir_r(d, &de, &entp);
1804 if (err != 0) {
1805 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1806 mod->name, strerror(-err));
1807 goto fail;
1808 }
1809
1810 if (de.d_name[0] == '.') {
1811 if (de.d_name[1] == '\0' ||
1812 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001813 continue;
1814 }
1815
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001816 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001817 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001818 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1819 dname, de.d_name);
1820 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001821 }
1822
1823 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001824 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001825 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001826 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1827 dname, de.d_name);
1828 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001829 }
1830
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001831 namesz = strlen(de.d_name) + 1;
1832 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001833
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001834 if (section == NULL) {
1835 ERR(mod->ctx, "out of memory\n");
1836 goto fail;
1837 }
1838
1839 section->address = address;
1840 memcpy(section->name, de.d_name, namesz);
1841
1842 l = kmod_list_append(list, section);
1843 if (l != NULL) {
1844 list = l;
1845 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001846 ERR(mod->ctx, "out of memory\n");
1847 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001848 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001849 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001850 }
1851
1852 closedir(d);
1853 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001854
1855fail:
1856 closedir(d);
1857 kmod_module_unref_list(list);
1858 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001859}
1860
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001861/**
1862 * kmod_module_section_get_module_name:
1863 * @entry: a list entry representing a kmod module section
1864 *
1865 * Get the name of a kmod module section.
1866 *
1867 * After use, free the @list by calling kmod_module_section_free_list().
1868 *
1869 * Returns: the name of this kmod module section on success or NULL on
1870 * failure. The string is owned by the section, do not free it.
1871 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001872KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1873{
1874 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001875
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001876 if (entry == NULL)
1877 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001878
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001879 section = entry->data;
1880 return section->name;
1881}
1882
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001883/**
1884 * kmod_module_section_get_address:
1885 * @entry: a list entry representing a kmod module section
1886 *
1887 * Get the address of a kmod module section.
1888 *
1889 * After use, free the @list by calling kmod_module_section_free_list().
1890 *
1891 * Returns: the address of this kmod module section on success or ULONG_MAX
1892 * on failure.
1893 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001894KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1895{
1896 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001897
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001898 if (entry == NULL)
1899 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001900
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001901 section = entry->data;
1902 return section->address;
1903}
1904
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001905/**
1906 * kmod_module_section_free_list:
1907 * @list: kmod module section list
1908 *
1909 * Release the resources taken by @list
1910 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001911KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1912{
1913 while (list) {
1914 kmod_module_section_free(list->data);
1915 list = kmod_list_remove(list);
1916 }
1917}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001918
1919struct kmod_module_info {
1920 char *key;
1921 char value[];
1922};
1923
1924static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
1925{
1926 struct kmod_module_info *info;
1927
1928 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
1929 if (info == NULL)
1930 return NULL;
1931
1932 info->key = (char *)info + sizeof(struct kmod_module_info)
1933 + valuelen + 1;
1934 memcpy(info->key, key, keylen);
1935 info->key[keylen] = '\0';
1936 memcpy(info->value, value, valuelen);
1937 info->value[valuelen] = '\0';
1938 return info;
1939}
1940
1941static void kmod_module_info_free(struct kmod_module_info *info)
1942{
1943 free(info);
1944}
1945
1946/**
1947 * kmod_module_get_info:
1948 * @mod: kmod module
1949 * @list: where to return list of module information. Use
1950 * kmod_module_info_get_key() and
1951 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02001952 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001953 *
1954 * Get a list of entries in ELF section ".modinfo", these contain
1955 * alias, license, depends, vermagic and other keys with respective
1956 * values.
1957 *
1958 * After use, free the @list by calling kmod_module_info_free_list().
1959 *
1960 * Returns: 0 on success or < 0 otherwise.
1961 */
1962KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
1963{
1964 struct kmod_file *file;
1965 struct kmod_elf *elf;
1966 const char *path;
1967 const void *mem;
1968 char **strings;
1969 size_t size;
1970 int i, count, ret = 0;
1971
1972 if (mod == NULL || list == NULL)
1973 return -ENOENT;
1974
1975 assert(*list == NULL);
1976
1977 path = kmod_module_get_path(mod);
1978 if (path == NULL)
1979 return -ENOENT;
1980
Lucas De Marchic68e92f2012-01-04 08:19:34 -02001981 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001982 if (file == NULL)
1983 return -errno;
1984
1985 size = kmod_file_get_size(file);
1986 mem = kmod_file_get_contents(file);
1987
1988 elf = kmod_elf_new(mem, size);
1989 if (elf == NULL) {
1990 ret = -errno;
1991 goto elf_open_error;
1992 }
1993
1994 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
1995 if (count < 0) {
1996 ret = count;
1997 goto get_strings_error;
1998 }
1999
2000 for (i = 0; i < count; i++) {
2001 struct kmod_module_info *info;
2002 struct kmod_list *n;
2003 const char *key, *value;
2004 size_t keylen, valuelen;
2005
2006 key = strings[i];
2007 value = strchr(key, '=');
2008 if (value == NULL) {
2009 keylen = strlen(key);
2010 valuelen = 0;
2011 } else {
2012 keylen = value - key;
2013 value++;
2014 valuelen = strlen(value);
2015 }
2016
2017 info = kmod_module_info_new(key, keylen, value, valuelen);
2018 if (info == NULL) {
2019 ret = -errno;
2020 kmod_module_info_free_list(*list);
2021 *list = NULL;
2022 goto list_error;
2023 }
2024
2025 n = kmod_list_append(*list, info);
2026 if (n != NULL)
2027 *list = n;
2028 else {
2029 kmod_module_info_free(info);
2030 kmod_module_info_free_list(*list);
2031 *list = NULL;
2032 ret = -ENOMEM;
2033 goto list_error;
2034 }
2035 }
2036 ret = count;
2037
2038list_error:
2039 free(strings);
2040get_strings_error:
2041 kmod_elf_unref(elf);
2042elf_open_error:
2043 kmod_file_unref(file);
2044
2045 return ret;
2046}
2047
2048/**
2049 * kmod_module_info_get_key:
2050 * @entry: a list entry representing a kmod module info
2051 *
2052 * Get the key of a kmod module info.
2053 *
2054 * Returns: the key of this kmod module info on success or NULL on
2055 * failure. The string is owned by the info, do not free it.
2056 */
2057KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2058{
2059 struct kmod_module_info *info;
2060
2061 if (entry == NULL)
2062 return NULL;
2063
2064 info = entry->data;
2065 return info->key;
2066}
2067
2068/**
2069 * kmod_module_info_get_value:
2070 * @entry: a list entry representing a kmod module info
2071 *
2072 * Get the value of a kmod module info.
2073 *
2074 * Returns: the value of this kmod module info on success or NULL on
2075 * failure. The string is owned by the info, do not free it.
2076 */
2077KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2078{
2079 struct kmod_module_info *info;
2080
2081 if (entry == NULL)
2082 return NULL;
2083
2084 info = entry->data;
2085 return info->value;
2086}
2087
2088/**
2089 * kmod_module_info_free_list:
2090 * @list: kmod module info list
2091 *
2092 * Release the resources taken by @list
2093 */
2094KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2095{
2096 while (list) {
2097 kmod_module_info_free(list->data);
2098 list = kmod_list_remove(list);
2099 }
2100}
2101
2102struct kmod_module_version {
2103 uint64_t crc;
2104 char symbol[];
2105};
2106
2107static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2108{
2109 struct kmod_module_version *mv;
2110 size_t symbollen = strlen(symbol) + 1;
2111
2112 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2113 if (mv == NULL)
2114 return NULL;
2115
2116 mv->crc = crc;
2117 memcpy(mv->symbol, symbol, symbollen);
2118 return mv;
2119}
2120
2121static void kmod_module_version_free(struct kmod_module_version *version)
2122{
2123 free(version);
2124}
2125
2126/**
2127 * kmod_module_get_versions:
2128 * @mod: kmod module
2129 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002130 * kmod_module_version_get_symbol() and
2131 * kmod_module_version_get_crc(). Release this list with
2132 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002133 *
2134 * Get a list of entries in ELF section "__versions".
2135 *
2136 * After use, free the @list by calling kmod_module_versions_free_list().
2137 *
2138 * Returns: 0 on success or < 0 otherwise.
2139 */
2140KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2141{
2142 struct kmod_file *file;
2143 struct kmod_elf *elf;
2144 const char *path;
2145 const void *mem;
2146 struct kmod_modversion *versions;
2147 size_t size;
2148 int i, count, ret = 0;
2149
2150 if (mod == NULL || list == NULL)
2151 return -ENOENT;
2152
2153 assert(*list == NULL);
2154
2155 path = kmod_module_get_path(mod);
2156 if (path == NULL)
2157 return -ENOENT;
2158
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002159 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002160 if (file == NULL)
2161 return -errno;
2162
2163 size = kmod_file_get_size(file);
2164 mem = kmod_file_get_contents(file);
2165
2166 elf = kmod_elf_new(mem, size);
2167 if (elf == NULL) {
2168 ret = -errno;
2169 goto elf_open_error;
2170 }
2171
2172 count = kmod_elf_get_modversions(elf, &versions);
2173 if (count < 0) {
2174 ret = count;
2175 goto get_strings_error;
2176 }
2177
2178 for (i = 0; i < count; i++) {
2179 struct kmod_module_version *mv;
2180 struct kmod_list *n;
2181
2182 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2183 if (mv == NULL) {
2184 ret = -errno;
2185 kmod_module_versions_free_list(*list);
2186 *list = NULL;
2187 goto list_error;
2188 }
2189
2190 n = kmod_list_append(*list, mv);
2191 if (n != NULL)
2192 *list = n;
2193 else {
2194 kmod_module_version_free(mv);
2195 kmod_module_versions_free_list(*list);
2196 *list = NULL;
2197 ret = -ENOMEM;
2198 goto list_error;
2199 }
2200 }
2201 ret = count;
2202
2203list_error:
2204 free(versions);
2205get_strings_error:
2206 kmod_elf_unref(elf);
2207elf_open_error:
2208 kmod_file_unref(file);
2209
2210 return ret;
2211}
2212
2213/**
2214 * kmod_module_versions_get_symbol:
2215 * @entry: a list entry representing a kmod module versions
2216 *
2217 * Get the symbol of a kmod module versions.
2218 *
2219 * Returns: the symbol of this kmod module versions on success or NULL
2220 * on failure. The string is owned by the versions, do not free it.
2221 */
2222KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2223{
2224 struct kmod_module_version *version;
2225
2226 if (entry == NULL)
2227 return NULL;
2228
2229 version = entry->data;
2230 return version->symbol;
2231}
2232
2233/**
2234 * kmod_module_version_get_crc:
2235 * @entry: a list entry representing a kmod module version
2236 *
2237 * Get the crc of a kmod module version.
2238 *
2239 * Returns: the crc of this kmod module version on success or NULL on
2240 * failure. The string is owned by the version, do not free it.
2241 */
2242KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2243{
2244 struct kmod_module_version *version;
2245
2246 if (entry == NULL)
2247 return 0;
2248
2249 version = entry->data;
2250 return version->crc;
2251}
2252
2253/**
2254 * kmod_module_versions_free_list:
2255 * @list: kmod module versions list
2256 *
2257 * Release the resources taken by @list
2258 */
2259KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2260{
2261 while (list) {
2262 kmod_module_version_free(list->data);
2263 list = kmod_list_remove(list);
2264 }
2265}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002266
2267struct kmod_module_symbol {
2268 uint64_t crc;
2269 char symbol[];
2270};
2271
2272static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2273{
2274 struct kmod_module_symbol *mv;
2275 size_t symbollen = strlen(symbol) + 1;
2276
2277 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2278 if (mv == NULL)
2279 return NULL;
2280
2281 mv->crc = crc;
2282 memcpy(mv->symbol, symbol, symbollen);
2283 return mv;
2284}
2285
2286static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2287{
2288 free(symbol);
2289}
2290
2291/**
2292 * kmod_module_get_symbols:
2293 * @mod: kmod module
2294 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002295 * kmod_module_symbol_get_symbol() and
2296 * kmod_module_symbol_get_crc(). Release this list with
2297 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002298 *
2299 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2300 *
2301 * After use, free the @list by calling kmod_module_symbols_free_list().
2302 *
2303 * Returns: 0 on success or < 0 otherwise.
2304 */
2305KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2306{
2307 struct kmod_file *file;
2308 struct kmod_elf *elf;
2309 const char *path;
2310 const void *mem;
2311 struct kmod_modversion *symbols;
2312 size_t size;
2313 int i, count, ret = 0;
2314
2315 if (mod == NULL || list == NULL)
2316 return -ENOENT;
2317
2318 assert(*list == NULL);
2319
2320 path = kmod_module_get_path(mod);
2321 if (path == NULL)
2322 return -ENOENT;
2323
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002324 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002325 if (file == NULL)
2326 return -errno;
2327
2328 size = kmod_file_get_size(file);
2329 mem = kmod_file_get_contents(file);
2330
2331 elf = kmod_elf_new(mem, size);
2332 if (elf == NULL) {
2333 ret = -errno;
2334 goto elf_open_error;
2335 }
2336
2337 count = kmod_elf_get_symbols(elf, &symbols);
2338 if (count < 0) {
2339 ret = count;
2340 goto get_strings_error;
2341 }
2342
2343 for (i = 0; i < count; i++) {
2344 struct kmod_module_symbol *mv;
2345 struct kmod_list *n;
2346
2347 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2348 if (mv == NULL) {
2349 ret = -errno;
2350 kmod_module_symbols_free_list(*list);
2351 *list = NULL;
2352 goto list_error;
2353 }
2354
2355 n = kmod_list_append(*list, mv);
2356 if (n != NULL)
2357 *list = n;
2358 else {
2359 kmod_module_symbol_free(mv);
2360 kmod_module_symbols_free_list(*list);
2361 *list = NULL;
2362 ret = -ENOMEM;
2363 goto list_error;
2364 }
2365 }
2366 ret = count;
2367
2368list_error:
2369 free(symbols);
2370get_strings_error:
2371 kmod_elf_unref(elf);
2372elf_open_error:
2373 kmod_file_unref(file);
2374
2375 return ret;
2376}
2377
2378/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002379 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002380 * @entry: a list entry representing a kmod module symbols
2381 *
2382 * Get the symbol of a kmod module symbols.
2383 *
2384 * Returns: the symbol of this kmod module symbols on success or NULL
2385 * on failure. The string is owned by the symbols, do not free it.
2386 */
2387KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2388{
2389 struct kmod_module_symbol *symbol;
2390
2391 if (entry == NULL)
2392 return NULL;
2393
2394 symbol = entry->data;
2395 return symbol->symbol;
2396}
2397
2398/**
2399 * kmod_module_symbol_get_crc:
2400 * @entry: a list entry representing a kmod module symbol
2401 *
2402 * Get the crc of a kmod module symbol.
2403 *
2404 * Returns: the crc of this kmod module symbol on success or NULL on
2405 * failure. The string is owned by the symbol, do not free it.
2406 */
2407KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2408{
2409 struct kmod_module_symbol *symbol;
2410
2411 if (entry == NULL)
2412 return 0;
2413
2414 symbol = entry->data;
2415 return symbol->crc;
2416}
2417
2418/**
2419 * kmod_module_symbols_free_list:
2420 * @list: kmod module symbols list
2421 *
2422 * Release the resources taken by @list
2423 */
2424KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2425{
2426 while (list) {
2427 kmod_module_symbol_free(list->data);
2428 list = kmod_list_remove(list);
2429 }
2430}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002431
2432struct kmod_module_dependency_symbol {
2433 uint64_t crc;
2434 uint8_t bind;
2435 char symbol[];
2436};
2437
2438static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2439{
2440 struct kmod_module_dependency_symbol *mv;
2441 size_t symbollen = strlen(symbol) + 1;
2442
2443 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2444 if (mv == NULL)
2445 return NULL;
2446
2447 mv->crc = crc;
2448 mv->bind = bind;
2449 memcpy(mv->symbol, symbol, symbollen);
2450 return mv;
2451}
2452
2453static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2454{
2455 free(dependency_symbol);
2456}
2457
2458/**
2459 * kmod_module_get_dependency_symbols:
2460 * @mod: kmod module
2461 * @list: where to return list of module dependency_symbols. Use
2462 * kmod_module_dependency_symbol_get_symbol() and
2463 * kmod_module_dependency_symbol_get_crc(). Release this list with
2464 * kmod_module_dependency_symbols_free_list()
2465 *
2466 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2467 *
2468 * After use, free the @list by calling
2469 * kmod_module_dependency_symbols_free_list().
2470 *
2471 * Returns: 0 on success or < 0 otherwise.
2472 */
2473KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2474{
2475 struct kmod_file *file;
2476 struct kmod_elf *elf;
2477 const char *path;
2478 const void *mem;
2479 struct kmod_modversion *symbols;
2480 size_t size;
2481 int i, count, ret = 0;
2482
2483 if (mod == NULL || list == NULL)
2484 return -ENOENT;
2485
2486 assert(*list == NULL);
2487
2488 path = kmod_module_get_path(mod);
2489 if (path == NULL)
2490 return -ENOENT;
2491
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002492 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002493 if (file == NULL)
2494 return -errno;
2495
2496 size = kmod_file_get_size(file);
2497 mem = kmod_file_get_contents(file);
2498
2499 elf = kmod_elf_new(mem, size);
2500 if (elf == NULL) {
2501 ret = -errno;
2502 goto elf_open_error;
2503 }
2504
2505 count = kmod_elf_get_dependency_symbols(elf, &symbols);
2506 if (count < 0) {
2507 ret = count;
2508 goto get_strings_error;
2509 }
2510
2511 for (i = 0; i < count; i++) {
2512 struct kmod_module_dependency_symbol *mv;
2513 struct kmod_list *n;
2514
2515 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2516 symbols[i].bind,
2517 symbols[i].symbol);
2518 if (mv == NULL) {
2519 ret = -errno;
2520 kmod_module_dependency_symbols_free_list(*list);
2521 *list = NULL;
2522 goto list_error;
2523 }
2524
2525 n = kmod_list_append(*list, mv);
2526 if (n != NULL)
2527 *list = n;
2528 else {
2529 kmod_module_dependency_symbol_free(mv);
2530 kmod_module_dependency_symbols_free_list(*list);
2531 *list = NULL;
2532 ret = -ENOMEM;
2533 goto list_error;
2534 }
2535 }
2536 ret = count;
2537
2538list_error:
2539 free(symbols);
2540get_strings_error:
2541 kmod_elf_unref(elf);
2542elf_open_error:
2543 kmod_file_unref(file);
2544
2545 return ret;
2546}
2547
2548/**
2549 * kmod_module_dependency_symbol_get_symbol:
2550 * @entry: a list entry representing a kmod module dependency_symbols
2551 *
2552 * Get the dependency symbol of a kmod module
2553 *
2554 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2555 * on failure. The string is owned by the dependency_symbols, do not free it.
2556 */
2557KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2558{
2559 struct kmod_module_dependency_symbol *dependency_symbol;
2560
2561 if (entry == NULL)
2562 return NULL;
2563
2564 dependency_symbol = entry->data;
2565 return dependency_symbol->symbol;
2566}
2567
2568/**
2569 * kmod_module_dependency_symbol_get_crc:
2570 * @entry: a list entry representing a kmod module dependency_symbol
2571 *
2572 * Get the crc of a kmod module dependency_symbol.
2573 *
2574 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2575 * failure. The string is owned by the dependency_symbol, do not free it.
2576 */
2577KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2578{
2579 struct kmod_module_dependency_symbol *dependency_symbol;
2580
2581 if (entry == NULL)
2582 return 0;
2583
2584 dependency_symbol = entry->data;
2585 return dependency_symbol->crc;
2586}
2587
2588/**
2589 * kmod_module_dependency_symbol_get_bind:
2590 * @entry: a list entry representing a kmod module dependency_symbol
2591 *
2592 * Get the bind type of a kmod module dependency_symbol.
2593 *
2594 * Returns: the bind of this kmod module dependency_symbol on success
2595 * or < 0 on failure.
2596 */
2597KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2598{
2599 struct kmod_module_dependency_symbol *dependency_symbol;
2600
2601 if (entry == NULL)
2602 return 0;
2603
2604 dependency_symbol = entry->data;
2605 return dependency_symbol->bind;
2606}
2607
2608/**
2609 * kmod_module_dependency_symbols_free_list:
2610 * @list: kmod module dependency_symbols list
2611 *
2612 * Release the resources taken by @list
2613 */
2614KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2615{
2616 while (list) {
2617 kmod_module_dependency_symbol_free(list->data);
2618 list = kmod_list_remove(list);
2619 }
2620}