blob: ac6b71a543eaf02205b9a8cb4cdb55b7c3eb29f4 [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 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 <ctype.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030023#include <dirent.h>
24#include <errno.h>
25#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020026#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020027#include <limits.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030028#include <stdarg.h>
29#include <stddef.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020034#include <sys/mman.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030035#include <sys/stat.h>
Kees Cook144d1822013-02-18 12:02:32 -080036#include <sys/syscall.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030037#include <sys/types.h>
Thierry Vignaudeff917c2012-01-17 17:32:48 -020038#include <sys/wait.h>
Kees Cook144d1822013-02-18 12:02:32 -080039#ifdef HAVE_LINUX_MODULE_H
40#include <linux/module.h>
41#endif
42
Lucas De Marchi96573a02014-10-03 00:01:35 -030043#include <shared/util.h>
44
Lucas De Marchi8f788d52011-11-25 01:22:56 -020045#include "libkmod.h"
Lucas De Marchi83b855a2013-07-04 16:13:11 -030046#include "libkmod-internal.h"
Lucas De Marchi8f788d52011-11-25 01:22:56 -020047
48/**
Lucas De Marchi66819512012-01-09 04:47:40 -020049 * SECTION:libkmod-module
50 * @short_description: operate on kernel modules
51 */
52
53/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020054 * kmod_module:
55 *
56 * Opaque object representing a module.
57 */
58struct kmod_module {
59 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020060 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020061 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020062 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020063 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020064 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020065 const char *install_commands; /* owned by kmod_config */
66 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020067 char *alias; /* only set if this module was created from an alias */
Lucas De Marchi1eff9422012-10-18 01:36:33 -030068 struct kmod_file *file;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020069 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020070 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020071 struct {
72 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020073 bool options : 1;
74 bool install_commands : 1;
75 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020076 } init;
Lucas De Marchib1a51252012-01-29 01:49:09 -020077
78 /*
79 * private field used by kmod_module_get_probe_list() to detect
80 * dependency loops
81 */
Lucas De Marchiece09aa2012-01-18 01:26:44 -020082 bool visited : 1;
Lucas De Marchi89e92482012-01-29 02:35:46 -020083
84 /*
85 * set by kmod_module_get_probe_list: indicates for probe_insert()
86 * whether the module's command and softdep should be ignored
87 */
88 bool ignorecmd : 1;
Lucas De Marchi38052742012-02-16 20:43:16 -020089
90 /*
Michal Marek450bd1b2014-03-31 15:18:50 +020091 * set by kmod_module_get_probe_list: indicates whether this is the
92 * module the user asked for or its dependency, or whether this
93 * is a softdep only
94 */
95 bool required : 1;
96
97 /*
Lucas De Marchi38052742012-02-16 20:43:16 -020098 * if module was created by searching the modules.builtin file, this
99 * is set. There's nothing much useful one can do with such a
100 * "module", except knowing it's builtin.
101 */
102 bool builtin : 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200103};
104
Lucas De Marchic35347f2011-12-12 10:48:02 -0200105static inline const char *path_join(const char *path, size_t prefixlen,
106 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200107{
108 size_t pathlen;
109
110 if (path[0] == '/')
111 return path;
112
113 pathlen = strlen(path);
114 if (prefixlen + pathlen + 1 >= PATH_MAX)
115 return NULL;
116
117 memcpy(buf + prefixlen, path, pathlen + 1);
118 return buf;
119}
120
Dave Reisneraf9572c2012-02-02 11:07:33 -0500121static inline bool module_is_inkernel(struct kmod_module *mod)
122{
123 int state = kmod_module_get_initstate(mod);
Lucas De Marchi38052742012-02-16 20:43:16 -0200124
Dave Reisneraf9572c2012-02-02 11:07:33 -0500125 if (state == KMOD_MODULE_LIVE ||
Dave Reisneraf9572c2012-02-02 11:07:33 -0500126 state == KMOD_MODULE_BUILTIN)
127 return true;
Lucas De Marchi38052742012-02-16 20:43:16 -0200128
129 return false;
Dave Reisneraf9572c2012-02-02 11:07:33 -0500130}
131
Lucas De Marchi671d4892011-12-05 20:23:05 -0200132int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200133{
134 struct kmod_ctx *ctx = mod->ctx;
135 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200136 const char *dirname;
137 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200138 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200139 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200140 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200141
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200142 if (mod->init.dep)
143 return mod->n_dep;
144 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200145 mod->init.dep = true;
146
147 p = strchr(line, ':');
148 if (p == NULL)
149 return 0;
150
Lucas De Marchi671d4892011-12-05 20:23:05 -0200151 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200152 dirname = kmod_get_dirname(mod->ctx);
153 dirnamelen = strlen(dirname);
154 if (dirnamelen + 2 >= PATH_MAX)
155 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200156
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200157 memcpy(buf, dirname, dirnamelen);
158 buf[dirnamelen] = '/';
159 dirnamelen++;
160 buf[dirnamelen] = '\0';
161
162 if (mod->path == NULL) {
163 const char *str = path_join(line, dirnamelen, buf);
164 if (str == NULL)
165 return 0;
166 mod->path = strdup(str);
167 if (mod->path == NULL)
168 return 0;
169 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200170
Lucas De Marchi7636e722011-12-01 17:56:03 -0200171 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200172 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
173 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200174 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200175 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200176
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200177 path = path_join(p, dirnamelen, buf);
178 if (path == NULL) {
179 ERR(ctx, "could not join path '%s' and '%s'.\n",
180 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200181 goto fail;
182 }
183
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200184 err = kmod_module_new_from_path(ctx, path, &depmod);
185 if (err < 0) {
186 ERR(ctx, "ctx=%p path=%s error=%s\n",
187 ctx, path, strerror(-err));
188 goto fail;
189 }
190
191 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200192
Lucas De Marchib94a7372011-12-26 20:10:49 -0200193 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200194 n++;
195 }
196
197 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
198
199 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200200 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200201 return n;
202
203fail:
204 kmod_module_unref_list(list);
205 mod->init.dep = false;
206 return err;
207}
208
Lucas De Marchiece09aa2012-01-18 01:26:44 -0200209void kmod_module_set_visited(struct kmod_module *mod, bool visited)
210{
211 mod->visited = visited;
212}
213
Lucas De Marchi38052742012-02-16 20:43:16 -0200214void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
215{
216 mod->builtin = builtin;
217}
218
Michal Marek450bd1b2014-03-31 15:18:50 +0200219void kmod_module_set_required(struct kmod_module *mod, bool required)
220{
221 mod->required = required;
222}
223
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200224/*
225 * Memory layout with alias:
226 *
227 * struct kmod_module {
228 * hashkey -----.
229 * alias -----. |
230 * name ----. | |
231 * } | | |
232 * name <----------' | |
233 * alias <-----------' |
234 * name\alias <--------'
235 *
236 * Memory layout without alias:
237 *
238 * struct kmod_module {
239 * hashkey ---.
240 * alias -----|----> NULL
241 * name ----. |
242 * } | |
243 * name <----------'-'
244 *
245 * @key is "name\alias" or "name" (in which case alias == NULL)
246 */
247static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
248 const char *name, size_t namelen,
249 const char *alias, size_t aliaslen,
250 struct kmod_module **mod)
251{
252 struct kmod_module *m;
253 size_t keylen;
254
255 m = kmod_pool_get_module(ctx, key);
256 if (m != NULL) {
257 *mod = kmod_module_ref(m);
258 return 0;
259 }
260
261 if (alias == NULL)
262 keylen = namelen;
263 else
264 keylen = namelen + aliaslen + 1;
265
266 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
Lucas De Marchi9f025612013-11-18 11:52:53 -0200267 if (m == NULL)
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200268 return -ENOMEM;
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200269
270 memset(m, 0, sizeof(*m));
271
272 m->ctx = kmod_ref(ctx);
273 m->name = (char *)m + sizeof(*m);
274 memcpy(m->name, key, keylen + 1);
275 if (alias == NULL) {
276 m->hashkey = m->name;
277 m->alias = NULL;
278 } else {
279 m->name[namelen] = '\0';
280 m->alias = m->name + namelen + 1;
281 m->hashkey = m->name + keylen + 1;
282 memcpy(m->hashkey, key, keylen + 1);
283 }
284
285 m->refcount = 1;
286 kmod_pool_add_module(ctx, m, m->hashkey);
287 *mod = m;
288
289 return 0;
290}
291
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200292/**
293 * kmod_module_new_from_name:
294 * @ctx: kmod library context
295 * @name: name of the module
296 * @mod: where to save the created struct kmod_module
297 *
298 * Create a new struct kmod_module using the module name. @name can not be an
299 * alias, file name or anything else; it must be a module name. There's no
Dan McGee9a252c22012-02-03 20:29:07 -0600300 * check if the module exists in the system.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200301 *
302 * This function is also used internally by many others that return a new
303 * struct kmod_module or a new list of modules.
304 *
305 * The initial refcount is 1, and needs to be decremented to release the
306 * resources of the kmod_module. Since libkmod keeps track of all
307 * kmod_modules created, they are all released upon @ctx destruction too. Do
308 * not unref @ctx before all the desired operations with the returned
309 * kmod_module are done.
310 *
311 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
312 * module name or if memory allocation failed.
313 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200314KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
315 const char *name,
316 struct kmod_module **mod)
317{
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200318 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200319 char name_norm[PATH_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200320
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200321 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200322 return -ENOENT;
323
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200324 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200325
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200326 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200327}
328
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200329int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
330 const char *name, struct kmod_module **mod)
331{
332 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200333 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200334 size_t namelen = strlen(name);
335 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200336
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200337 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200338 return -ENAMETOOLONG;
339
340 memcpy(key, name, namelen);
341 memcpy(key + namelen + 1, alias, aliaslen + 1);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200342 key[namelen] = '\\';
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200343
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200344 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200345 if (err < 0)
346 return err;
347
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200348 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200349}
350
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200351/**
352 * kmod_module_new_from_path:
353 * @ctx: kmod library context
354 * @path: path where to find the given module
355 * @mod: where to save the created struct kmod_module
356 *
357 * Create a new struct kmod_module using the module path. @path must be an
358 * existent file with in the filesystem and must be accessible to libkmod.
359 *
360 * The initial refcount is 1, and needs to be decremented to release the
361 * resources of the kmod_module. Since libkmod keeps track of all
362 * kmod_modules created, they are all released upon @ctx destruction too. Do
363 * not unref @ctx before all the desired operations with the returned
364 * kmod_module are done.
365 *
366 * If @path is relative, it's treated as relative to the current working
367 * directory. Otherwise, give an absolute path.
368 *
369 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
370 * it's not a valid file for a kmod_module or if memory allocation failed.
371 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200372KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
373 const char *path,
374 struct kmod_module **mod)
375{
376 struct kmod_module *m;
377 int err;
378 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200379 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200380 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200381 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200382
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200383 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200384 return -ENOENT;
385
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200386 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200387 if (abspath == NULL) {
388 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200389 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200390 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200391
392 err = stat(abspath, &st);
393 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200394 err = -errno;
395 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200396 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200397 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200398 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200399
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200400 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200401 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200402 free(abspath);
403 return -ENOENT;
404 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200405
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200406 m = kmod_pool_get_module(ctx, name);
407 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200408 if (m->path == NULL)
409 m->path = abspath;
410 else if (streq(m->path, abspath))
411 free(abspath);
412 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200413 ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200414 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200415 free(abspath);
416 return -EEXIST;
417 }
418
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200419 *mod = kmod_module_ref(m);
420 return 0;
421 }
422
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200423 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
Leandro Pereirac1bc88c2014-04-28 21:02:45 -0300424 if (err < 0) {
425 free(abspath);
Lucas De Marchi9c7f3ad2012-01-29 15:40:58 -0200426 return err;
Leandro Pereirac1bc88c2014-04-28 21:02:45 -0300427 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200428
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200429 m->path = abspath;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200430 *mod = m;
431
432 return 0;
433}
434
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200435/**
436 * kmod_module_unref:
437 * @mod: kmod module
438 *
439 * Drop a reference of the kmod module. If the refcount reaches zero, its
440 * resources are released.
441 *
442 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
443 * returns the passed @mod with its refcount decremented.
444 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200445KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
446{
447 if (mod == NULL)
448 return NULL;
449
450 if (--mod->refcount > 0)
451 return mod;
452
453 DBG(mod->ctx, "kmod_module %p released\n", mod);
454
Lucas De Marchi4084c172011-12-15 13:43:22 -0200455 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200456 kmod_module_unref_list(mod->dep);
Lucas De Marchi1eff9422012-10-18 01:36:33 -0300457
458 if (mod->file)
459 kmod_file_unref(mod->file);
460
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200461 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200462 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200463 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200464 free(mod);
465 return NULL;
466}
467
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200468/**
469 * kmod_module_ref:
470 * @mod: kmod module
471 *
472 * Take a reference of the kmod module, incrementing its refcount.
473 *
474 * Returns: the passed @module with its refcount incremented.
475 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200476KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
477{
478 if (mod == NULL)
479 return NULL;
480
481 mod->refcount++;
482
483 return mod;
484}
485
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200486#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
487 do { \
488 if ((_err) < 0) \
489 goto _label_err; \
490 if (*(_list) != NULL) \
491 goto finish; \
492 } while (0)
493
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200494/**
495 * kmod_module_new_from_lookup:
496 * @ctx: kmod library context
497 * @given_alias: alias to look for
498 * @list: an empty list where to save the list of modules matching
499 * @given_alias
500 *
501 * Create a new list of kmod modules using an alias or module name and lookup
502 * libkmod's configuration files and indexes in order to find the module.
503 * Once it's found in one of the places, it stops searching and create the
504 * list of modules that is saved in @list.
505 *
506 * The search order is: 1. aliases in configuration file; 2. module names in
507 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
508 * in modules.alias index.
509 *
510 * The initial refcount is 1, and needs to be decremented to release the
511 * resources of the kmod_module. The returned @list must be released by
512 * calling kmod_module_unref_list(). Since libkmod keeps track of all
513 * kmod_modules created, they are all released upon @ctx destruction too. Do
514 * not unref @ctx before all the desired operations with the returned list are
515 * completed.
516 *
517 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
518 * methods failed, which is basically due to memory allocation fail. If module
519 * is not found, it still returns 0, but @list is an empty list.
520 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200521KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200522 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200523 struct kmod_list **list)
524{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200525 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200526 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200527
Lucas De Marchi4308b172011-12-13 10:26:04 -0200528 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200529 return -ENOENT;
530
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200531 if (list == NULL || *list != NULL) {
532 ERR(ctx, "An empty list is needed to create lookup\n");
533 return -ENOSYS;
534 }
535
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200536 if (alias_normalize(given_alias, alias, NULL) < 0) {
537 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200538 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200539 }
540
541 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200542
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200543 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200544 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200545 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200546
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200547 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200548 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
549 CHECK_ERR_AND_FINISH(err, fail, list, finish);
550
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200551 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200552 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
553 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200554
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200555 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200556 err = kmod_lookup_alias_from_commands(ctx, alias, list);
557 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200558
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200559 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200560 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
561 CHECK_ERR_AND_FINISH(err, fail, list, finish);
562
Lucas De Marchi38052742012-02-16 20:43:16 -0200563 DBG(ctx, "lookup modules.builtin %s\n", alias);
564 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
565 CHECK_ERR_AND_FINISH(err, fail, list, finish);
566
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200567finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200568 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200569 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200570fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200571 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200572 kmod_module_unref_list(*list);
573 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200574 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200575}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200576#undef CHECK_ERR_AND_FINISH
577
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200578/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200579 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200580 * @list: list of kmod modules
581 *
582 * Drop a reference of each kmod module in @list and releases the resources
583 * taken by the list itself.
584 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800585 * Returns: 0
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200586 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200587KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
588{
589 for (; list != NULL; list = kmod_list_remove(list))
590 kmod_module_unref(list->data);
591
592 return 0;
593}
594
Lucas De Marchi0d467432011-12-31 11:15:52 -0200595/**
596 * kmod_module_get_filtered_blacklist:
597 * @ctx: kmod library context
598 * @input: list of kmod_module to be filtered with blacklist
599 * @output: where to save the new list
600 *
Kay Sievers471a7d02012-04-14 20:47:47 +0200601 * This function should not be used. Use kmod_module_apply_filter instead.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500602 *
Lucas De Marchi0d467432011-12-31 11:15:52 -0200603 * Given a list @input, this function filter it out with config's blacklist
Dave Reisnerd80b1032012-02-24 10:05:11 -0500604 * and save it in @output.
Lucas De Marchi0d467432011-12-31 11:15:52 -0200605 *
606 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
607 * list.
608 */
609KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
610 const struct kmod_list *input,
611 struct kmod_list **output)
612{
Dave Reisnerd80b1032012-02-24 10:05:11 -0500613 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
Lucas De Marchi0d467432011-12-31 11:15:52 -0200614}
615
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200616static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
617{
618 if (!mod->init.dep) {
619 /* lazy init */
620 char *line = kmod_search_moddep(mod->ctx, mod->name);
621
622 if (line == NULL)
623 return NULL;
624
625 kmod_module_parse_depline((struct kmod_module *)mod, line);
626 free(line);
627
628 if (!mod->init.dep)
629 return NULL;
630 }
631
632 return mod->dep;
633}
634
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200635/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200636 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200637 * @mod: kmod module
638 *
639 * Search the modules.dep index to find the dependencies of the given @mod.
640 * The result is cached in @mod, so subsequent calls to this function will
641 * return the already searched list of modules.
642 *
Chengwei Yang491c4902013-05-04 17:07:02 +0800643 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
644 * that can be released by calling kmod_module_unref_list().
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200645 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200646KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200647{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200648 struct kmod_list *l, *l_new, *list_new = NULL;
649
650 if (mod == NULL)
651 return NULL;
652
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200653 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200654
655 kmod_list_foreach(l, mod->dep) {
656 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
657 if (l_new == NULL) {
658 kmod_module_unref(l->data);
659 goto fail;
660 }
661
662 list_new = l_new;
663 }
664
665 return list_new;
666
667fail:
668 ERR(mod->ctx, "out of memory\n");
669 kmod_module_unref_list(list_new);
670 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200671}
672
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200673/**
674 * kmod_module_get_module:
675 * @entry: an entry in a list of kmod modules.
676 *
677 * Get the kmod module of this @entry in the list, increasing its refcount.
678 * After it's used, unref it. Since the refcount is incremented upon return,
679 * you still have to call kmod_module_unref_list() to release the list of kmod
680 * modules.
681 *
682 * Returns: NULL on failure or the kmod_module contained in this list entry
683 * with its refcount incremented.
684 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200685KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200686{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200687 if (entry == NULL)
688 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200689
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200690 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200691}
692
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200693/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200694 * kmod_module_get_name:
695 * @mod: kmod module
696 *
697 * Get the name of this kmod module. Name is always available, independently
698 * if it was created by kmod_module_new_from_name() or another function and
699 * it's always normalized (dashes are replaced with underscores).
700 *
701 * Returns: the name of this kmod module.
702 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200703KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200704{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200705 if (mod == NULL)
706 return NULL;
707
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200708 return mod->name;
709}
710
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200711/**
712 * kmod_module_get_path:
713 * @mod: kmod module
714 *
715 * Get the path of this kmod module. If this kmod module was not created by
716 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200717 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200718 *
719 * Returns: the path of this kmod module or NULL if such information is not
720 * available.
721 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200722KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200723{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200724 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200725
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200726 if (mod == NULL)
727 return NULL;
728
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200729 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200730
Lucas De Marchie005fac2011-12-08 10:42:34 -0200731 if (mod->path != NULL)
732 return mod->path;
733 if (mod->init.dep)
734 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200735
Lucas De Marchie005fac2011-12-08 10:42:34 -0200736 /* lazy init */
737 line = kmod_search_moddep(mod->ctx, mod->name);
738 if (line == NULL)
739 return NULL;
740
741 kmod_module_parse_depline((struct kmod_module *) mod, line);
742 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200743
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200744 return mod->path;
745}
746
747
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200748extern long delete_module(const char *name, unsigned int flags);
749
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200750/**
751 * kmod_module_remove_module:
752 * @mod: kmod module
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500753 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
Chengwei Yangd7152f62013-05-04 17:07:03 +0800754 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
755 * use by a kernel subsystem or other process;
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500756 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
757 * delete_module(2).
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200758 *
759 * Remove a module from Linux kernel.
760 *
761 * Returns: 0 on success or < 0 on failure.
762 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200763KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
764 unsigned int flags)
765{
766 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200767
768 if (mod == NULL)
769 return -ENOENT;
770
Lucas De Marchi7ab88042013-09-20 01:30:07 -0500771 /* Filter out other flags and force ONONBLOCK */
772 flags &= KMOD_REMOVE_FORCE;
773 flags |= KMOD_REMOVE_NOWAIT;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200774
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200775 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200776 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200777 err = -errno;
778 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200779 }
780
Lucas De Marchiba998b92012-01-11 00:08:14 -0200781 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200782}
783
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200784extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200785
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200786/**
787 * kmod_module_insert_module:
788 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200789 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +0800790 * behavior of this function, valid flags are
791 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
792 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200793 * @options: module's options to pass to Linux Kernel.
794 *
795 * Insert a module in Linux kernel. It opens the file pointed by @mod,
796 * mmap'ing it and passing to kernel.
797 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200798 * Returns: 0 on success or < 0 on failure. If module is already loaded it
799 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200800 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200801KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200802 unsigned int flags,
803 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200804{
805 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200806 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200807 off_t size;
Michal Marekc2f4d852014-02-28 13:05:32 +0100808 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200809 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200810 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200811
812 if (mod == NULL)
813 return -ENOENT;
814
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200815 path = kmod_module_get_path(mod);
816 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500817 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200818 return -ENOSYS;
819 }
820
Michal Marekc2f4d852014-02-28 13:05:32 +0100821 mod->file = kmod_file_open(mod->ctx, path);
822 if (mod->file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200823 err = -errno;
824 return err;
825 }
826
Michal Marekc2f4d852014-02-28 13:05:32 +0100827 if (kmod_file_get_direct(mod->file)) {
Kees Cook144d1822013-02-18 12:02:32 -0800828 unsigned int kernel_flags = 0;
829
Kees Cook144d1822013-02-18 12:02:32 -0800830 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
831 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
832 if (flags & KMOD_INSERT_FORCE_MODVERSION)
833 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
Kees Cook144d1822013-02-18 12:02:32 -0800834
Michal Marekc2f4d852014-02-28 13:05:32 +0100835 err = finit_module(kmod_file_get_fd(mod->file), args, kernel_flags);
Kees Cook144d1822013-02-18 12:02:32 -0800836 if (err == 0 || errno != ENOSYS)
837 goto init_finished;
838 }
839
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200840 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
Michal Marekc2f4d852014-02-28 13:05:32 +0100841 elf = kmod_file_get_elf(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200842 if (elf == NULL) {
843 err = -errno;
Michal Marekc2f4d852014-02-28 13:05:32 +0100844 return err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200845 }
846
847 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
848 err = kmod_elf_strip_section(elf, "__versions");
849 if (err < 0)
850 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
851 }
852
853 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
854 err = kmod_elf_strip_vermagic(elf);
855 if (err < 0)
856 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
857 }
858
859 mem = kmod_elf_get_memory(elf);
Michal Marekc2f4d852014-02-28 13:05:32 +0100860 } else {
861 mem = kmod_file_get_contents(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200862 }
Michal Marekc2f4d852014-02-28 13:05:32 +0100863 size = kmod_file_get_size(mod->file);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200864
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200865 err = init_module(mem, size, args);
Kees Cook144d1822013-02-18 12:02:32 -0800866init_finished:
Lucas De Marchibbf59322011-12-30 14:13:33 -0200867 if (err < 0) {
868 err = -errno;
869 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
870 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200871 return err;
872}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200873
Lucas De Marchiddbda022011-12-27 11:40:10 -0200874static bool module_is_blacklisted(struct kmod_module *mod)
875{
876 struct kmod_ctx *ctx = mod->ctx;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -0300877 const struct kmod_config *config = kmod_get_config(ctx);
878 const struct kmod_list *bl = config->blacklists;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200879 const struct kmod_list *l;
880
881 kmod_list_foreach(l, bl) {
882 const char *modname = kmod_blacklist_get_modname(l);
883
884 if (streq(modname, mod->name))
885 return true;
886 }
887
888 return false;
889}
890
Dave Reisnerd80b1032012-02-24 10:05:11 -0500891/**
892 * kmod_module_apply_filter
893 * @ctx: kmod library context
Chengwei Yangd7152f62013-05-04 17:07:03 +0800894 * @filter_type: bitmask to filter modules out, valid types are
895 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
896 * KMOD_FILTER_BUILTIN: filter builtin modules out.
Dave Reisnerd80b1032012-02-24 10:05:11 -0500897 * @input: list of kmod_module to be filtered
898 * @output: where to save the new list
899 *
900 * Given a list @input, this function filter it out by the filter mask
901 * and save it in @output.
902 *
903 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
904 * list.
905 */
906KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
907 enum kmod_filter filter_type,
908 const struct kmod_list *input,
909 struct kmod_list **output)
910{
911 const struct kmod_list *li;
912
913 if (ctx == NULL || output == NULL)
914 return -ENOENT;
915
916 *output = NULL;
917 if (input == NULL)
918 return 0;
919
920 kmod_list_foreach(li, input) {
921 struct kmod_module *mod = li->data;
922 struct kmod_list *node;
923
924 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
925 module_is_blacklisted(mod))
926 continue;
927
Dave Reisnerbdda7e12012-02-24 23:02:06 -0500928 if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin)
Dave Reisnerd80b1032012-02-24 10:05:11 -0500929 continue;
930
931 node = kmod_list_append(*output, mod);
932 if (node == NULL)
933 goto fail;
934
935 *output = node;
936 kmod_module_ref(mod);
937 }
938
939 return 0;
940
941fail:
942 kmod_module_unref_list(*output);
943 *output = NULL;
944 return -ENOMEM;
945}
946
Lucas De Marchiddbda022011-12-27 11:40:10 -0200947static int command_do(struct kmod_module *mod, const char *type,
948 const char *cmd)
949{
950 const char *modname = kmod_module_get_name(mod);
951 int err;
952
953 DBG(mod->ctx, "%s %s\n", type, cmd);
954
955 setenv("MODPROBE_MODULE", modname, 1);
956 err = system(cmd);
957 unsetenv("MODPROBE_MODULE");
958
959 if (err == -1 || WEXITSTATUS(err)) {
960 ERR(mod->ctx, "Error running %s command for %s\n",
961 type, modname);
962 if (err != -1)
963 err = -WEXITSTATUS(err);
964 }
965
966 return err;
967}
968
Lucas De Marchib1a51252012-01-29 01:49:09 -0200969struct probe_insert_cb {
970 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
971 void *data;
972};
973
Lucas De Marchiddbda022011-12-27 11:40:10 -0200974static int module_do_install_commands(struct kmod_module *mod,
975 const char *options,
976 struct probe_insert_cb *cb)
977{
978 const char *command = kmod_module_get_install_commands(mod);
Lucas De Marchi9f025612013-11-18 11:52:53 -0200979 char *p;
980 _cleanup_free_ char *cmd;
Lucas De Marchiddbda022011-12-27 11:40:10 -0200981 int err;
982 size_t cmdlen, options_len, varlen;
983
984 assert(command);
985
986 if (options == NULL)
987 options = "";
988
989 options_len = strlen(options);
990 cmdlen = strlen(command);
991 varlen = sizeof("$CMDLINE_OPTS") - 1;
992
993 cmd = memdup(command, cmdlen + 1);
994 if (cmd == NULL)
995 return -ENOMEM;
996
997 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
998 size_t prefixlen = p - cmd;
999 size_t suffixlen = cmdlen - prefixlen - varlen;
1000 size_t slen = cmdlen - varlen + options_len;
1001 char *suffix = p + varlen;
1002 char *s = malloc(slen + 1);
Lucas De Marchi9f025612013-11-18 11:52:53 -02001003 if (!s)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001004 return -ENOMEM;
Lucas De Marchi9f025612013-11-18 11:52:53 -02001005
Lucas De Marchiddbda022011-12-27 11:40:10 -02001006 memcpy(s, cmd, p - cmd);
1007 memcpy(s + prefixlen, options, options_len);
1008 memcpy(s + prefixlen + options_len, suffix, suffixlen);
1009 s[slen] = '\0';
1010
1011 free(cmd);
1012 cmd = s;
1013 cmdlen = slen;
1014 }
1015
1016 if (cb->run_install != NULL)
1017 err = cb->run_install(mod, cmd, cb->data);
1018 else
1019 err = command_do(mod, "install", cmd);
1020
Lucas De Marchiddbda022011-12-27 11:40:10 -02001021 return err;
1022}
1023
Lucas De Marchiddbda022011-12-27 11:40:10 -02001024static char *module_options_concat(const char *opt, const char *xopt)
1025{
1026 // TODO: we might need to check if xopt overrides options on opt
1027 size_t optlen = opt == NULL ? 0 : strlen(opt);
1028 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1029 char *r;
1030
1031 if (optlen == 0 && xoptlen == 0)
1032 return NULL;
1033
1034 r = malloc(optlen + xoptlen + 2);
1035
1036 if (opt != NULL) {
1037 memcpy(r, opt, optlen);
1038 r[optlen] = ' ';
1039 optlen++;
1040 }
1041
1042 if (xopt != NULL)
1043 memcpy(r + optlen, xopt, xoptlen);
1044
1045 r[optlen + xoptlen] = '\0';
1046
1047 return r;
1048}
1049
Lucas De Marchib1a51252012-01-29 01:49:09 -02001050static int __kmod_module_get_probe_list(struct kmod_module *mod,
Michal Marek450bd1b2014-03-31 15:18:50 +02001051 bool required,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001052 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001053 struct kmod_list **list);
1054
1055/* re-entrant */
1056static int __kmod_module_fill_softdep(struct kmod_module *mod,
1057 struct kmod_list **list)
Lucas De Marchiddbda022011-12-27 11:40:10 -02001058{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001059 struct kmod_list *pre = NULL, *post = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001060 int err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001061
1062 err = kmod_module_get_softdeps(mod, &pre, &post);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001063 if (err < 0) {
Lucas De Marchi050db082012-02-18 03:56:21 -02001064 ERR(mod->ctx, "could not get softdep: %s\n",
1065 strerror(-err));
Lucas De Marchib1a51252012-01-29 01:49:09 -02001066 goto fail;
1067 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001068
Lucas De Marchib1a51252012-01-29 01:49:09 -02001069 kmod_list_foreach(l, pre) {
1070 struct kmod_module *m = l->data;
Michal Marek450bd1b2014-03-31 15:18:50 +02001071 err = __kmod_module_get_probe_list(m, false, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001072 if (err < 0)
1073 goto fail;
1074 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001075
Lucas De Marchib1a51252012-01-29 01:49:09 -02001076 l = kmod_list_append(*list, kmod_module_ref(mod));
1077 if (l == NULL) {
1078 kmod_module_unref(mod);
1079 err = -ENOMEM;
1080 goto fail;
1081 }
1082 *list = l;
Lucas De Marchi89e92482012-01-29 02:35:46 -02001083 mod->ignorecmd = (pre != NULL || post != NULL);
Lucas De Marchiddbda022011-12-27 11:40:10 -02001084
Lucas De Marchib1a51252012-01-29 01:49:09 -02001085 kmod_list_foreach(l, post) {
1086 struct kmod_module *m = l->data;
Michal Marek450bd1b2014-03-31 15:18:50 +02001087 err = __kmod_module_get_probe_list(m, false, false, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001088 if (err < 0)
1089 goto fail;
1090 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001091
Lucas De Marchib1a51252012-01-29 01:49:09 -02001092fail:
Lucas De Marchiddbda022011-12-27 11:40:10 -02001093 kmod_module_unref_list(pre);
1094 kmod_module_unref_list(post);
1095
1096 return err;
1097}
1098
Lucas De Marchib1a51252012-01-29 01:49:09 -02001099/* re-entrant */
1100static int __kmod_module_get_probe_list(struct kmod_module *mod,
Michal Marek450bd1b2014-03-31 15:18:50 +02001101 bool required,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001102 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001103 struct kmod_list **list)
1104{
1105 struct kmod_list *dep, *l;
1106 int err = 0;
1107
1108 if (mod->visited) {
1109 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1110 mod->name);
1111 return 0;
1112 }
Lucas De Marchi8cd0f9e2012-02-11 19:45:29 -02001113 mod->visited = true;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001114
1115 dep = kmod_module_get_dependencies(mod);
Michal Marek450bd1b2014-03-31 15:18:50 +02001116 if (required) {
1117 /*
1118 * Called from kmod_module_probe_insert_module(); set the
1119 * ->required flag on mod and all its dependencies before
1120 * they are possibly visited through some softdeps.
1121 */
1122 mod->required = true;
1123 kmod_list_foreach(l, dep) {
1124 struct kmod_module *m = l->data;
1125 m->required = true;
1126 }
1127 }
1128
Lucas De Marchib1a51252012-01-29 01:49:09 -02001129 kmod_list_foreach(l, dep) {
1130 struct kmod_module *m = l->data;
1131 err = __kmod_module_fill_softdep(m, list);
1132 if (err < 0)
Lucas De Marchi89e92482012-01-29 02:35:46 -02001133 goto finish;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001134 }
1135
Lucas De Marchi89e92482012-01-29 02:35:46 -02001136 if (ignorecmd) {
1137 l = kmod_list_append(*list, kmod_module_ref(mod));
1138 if (l == NULL) {
1139 kmod_module_unref(mod);
1140 err = -ENOMEM;
1141 goto finish;
1142 }
1143 *list = l;
1144 mod->ignorecmd = true;
1145 } else
1146 err = __kmod_module_fill_softdep(mod, list);
1147
Lucas De Marchib1a51252012-01-29 01:49:09 -02001148finish:
1149 kmod_module_unref_list(dep);
1150 return err;
1151}
1152
1153static int kmod_module_get_probe_list(struct kmod_module *mod,
Lucas De Marchi89e92482012-01-29 02:35:46 -02001154 bool ignorecmd,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001155 struct kmod_list **list)
1156{
1157 int err;
1158
1159 assert(mod != NULL);
1160 assert(list != NULL && *list == NULL);
1161
1162 /*
1163 * Make sure we don't get screwed by previous calls to this function
1164 */
1165 kmod_set_modules_visited(mod->ctx, false);
Michal Marek450bd1b2014-03-31 15:18:50 +02001166 kmod_set_modules_required(mod->ctx, false);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001167
Michal Marek450bd1b2014-03-31 15:18:50 +02001168 err = __kmod_module_get_probe_list(mod, true, ignorecmd, list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001169 if (err < 0) {
1170 kmod_module_unref_list(*list);
1171 *list = NULL;
1172 }
1173
1174 return err;
1175}
1176
Lucas De Marchiddbda022011-12-27 11:40:10 -02001177/**
1178 * kmod_module_probe_insert_module:
1179 * @mod: kmod module
1180 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
Chengwei Yangd7152f62013-05-04 17:07:03 +08001181 * behavior of this function, valid flags are
1182 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1183 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1184 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1185 * commands and softdeps configured in the system;
1186 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1187 * live in kernel or not;
1188 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1189 * associated callback function;
1190 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1191 * and the module is already live in kernel, the function will fail if this
1192 * flag is specified;
1193 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1194 * filter to this module and its dependencies. If any of the dependencies (or
1195 * the module) is blacklisted, the probe will fail, unless the blacklisted
1196 * module is already live in kernel;
1197 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1198 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1199 * alias and is blacklisted.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001200 * @extra_options: module's options to pass to Linux Kernel. It applies only
1201 * to @mod, not to its dependencies.
1202 * @run_install: function to run when @mod is backed by an install command.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001203 * @data: data to give back to @run_install callback
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001204 * @print_action: function to call with the action being taken (install or
1205 * insmod). It's useful for tools like modprobe when running with verbose
1206 * output or in dry-run mode.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001207 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001208 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
Lucas De Marchiddbda022011-12-27 11:40:10 -02001209 * install commands and applying blacklist.
1210 *
Lucas De Marchi7aed4602012-01-31 12:05:36 -02001211 * If @run_install is NULL, this function will fork and exec by calling
1212 * system(3). Don't pass a NULL argument in @run_install if your binary is
1213 * setuid/setgid (see warning in system(3)). If you need control over the
1214 * execution of an install command, give a callback function instead.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001215 *
Lucas De Marchib1a51252012-01-29 01:49:09 -02001216 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1217 * failure.
Lucas De Marchiddbda022011-12-27 11:40:10 -02001218 */
1219KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1220 unsigned int flags, const char *extra_options,
1221 int (*run_install)(struct kmod_module *m,
1222 const char *cmd, void *data),
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001223 const void *data,
1224 void (*print_action)(struct kmod_module *m,
1225 bool install,
1226 const char *options))
Lucas De Marchiddbda022011-12-27 11:40:10 -02001227{
Lucas De Marchib1a51252012-01-29 01:49:09 -02001228 struct kmod_list *list = NULL, *l;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001229 struct probe_insert_cb cb;
Lucas De Marchib1a51252012-01-29 01:49:09 -02001230 int err;
1231
1232 if (mod == NULL)
1233 return -ENOENT;
1234
Lucas De Marchi269de2e2012-02-07 09:48:59 -02001235 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1236 && module_is_inkernel(mod)) {
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001237 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
Dave Reisneraf9572c2012-02-02 11:07:33 -05001238 return -EEXIST;
1239 else
1240 return 0;
1241 }
1242
Lucas De Marchi68820172012-08-17 09:38:05 -03001243 /*
1244 * Ugly assignement + check. We need to check if we were told to check
1245 * blacklist and also return the reason why we failed.
1246 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1247 * module is an alias, so we also need to check it
1248 */
1249 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1250 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1251 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
Lucas De Marchib1a51252012-01-29 01:49:09 -02001252 if (module_is_blacklisted(mod))
1253 return err;
1254 }
1255
Lucas De Marchi89e92482012-01-29 02:35:46 -02001256 err = kmod_module_get_probe_list(mod,
1257 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001258 if (err < 0)
1259 return err;
1260
1261 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1262 struct kmod_list *filtered = NULL;
1263
Dave Reisnerd80b1032012-02-24 10:05:11 -05001264 err = kmod_module_apply_filter(mod->ctx,
1265 KMOD_FILTER_BLACKLIST, list, &filtered);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001266 if (err < 0)
1267 return err;
1268
1269 kmod_module_unref_list(list);
1270 if (filtered == NULL)
1271 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1272
1273 list = filtered;
1274 }
Lucas De Marchiddbda022011-12-27 11:40:10 -02001275
1276 cb.run_install = run_install;
1277 cb.data = (void *) data;
1278
Lucas De Marchib1a51252012-01-29 01:49:09 -02001279 kmod_list_foreach(l, list) {
1280 struct kmod_module *m = l->data;
1281 const char *moptions = kmod_module_get_options(m);
1282 const char *cmd = kmod_module_get_install_commands(m);
Lucas De Marchiabd55572012-02-19 04:20:30 -02001283 char *options;
1284
1285 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1286 && module_is_inkernel(m)) {
1287 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1288 m->name);
1289 err = -EEXIST;
1290 goto finish_module;
1291 }
1292
1293 options = module_options_concat(moptions,
Lucas De Marchib1a51252012-01-29 01:49:09 -02001294 m == mod ? extra_options : NULL);
1295
Lucas De Marchi89e92482012-01-29 02:35:46 -02001296 if (cmd != NULL && !m->ignorecmd) {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001297 if (print_action != NULL)
1298 print_action(m, true, options ?: "");
1299
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001300 if (!(flags & KMOD_PROBE_DRY_RUN))
1301 err = module_do_install_commands(m, options,
1302 &cb);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001303 } else {
Lucas De Marchi6bd07132012-01-30 17:02:06 -02001304 if (print_action != NULL)
1305 print_action(m, false, options ?: "");
1306
Lucas De Marchi4c1ffb72012-01-30 19:00:58 -02001307 if (!(flags & KMOD_PROBE_DRY_RUN))
1308 err = kmod_module_insert_module(m, flags,
1309 options);
Lucas De Marchib1a51252012-01-29 01:49:09 -02001310 }
1311
1312 free(options);
1313
Lucas De Marchiabd55572012-02-19 04:20:30 -02001314finish_module:
Lucas De Marchib1a51252012-01-29 01:49:09 -02001315 /*
Lucas De Marchi5f351472012-01-30 16:26:52 -02001316 * Treat "already loaded" error. If we were told to stop on
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001317 * already loaded and the module being loaded is not a softdep
1318 * or dep, bail out. Otherwise, just ignore and continue.
Lucas De Marchi5f351472012-01-30 16:26:52 -02001319 *
1320 * We need to check here because of race conditions. We
1321 * checked first if module was already loaded but it may have
1322 * been loaded between the check and the moment we try to
1323 * insert it.
Lucas De Marchib1a51252012-01-29 01:49:09 -02001324 */
Lucas De Marchi5f351472012-01-30 16:26:52 -02001325 if (err == -EEXIST && m == mod &&
Lucas De Marchi814a57b2012-02-06 12:46:39 -02001326 (flags & KMOD_PROBE_FAIL_ON_LOADED))
Lucas De Marchi5f351472012-01-30 16:26:52 -02001327 break;
Lucas De Marchi5f351472012-01-30 16:26:52 -02001328
Michal Marek450bd1b2014-03-31 15:18:50 +02001329 /*
1330 * Ignore errors from softdeps
1331 */
1332 if (err == -EEXIST || !m->required)
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001333 err = 0;
Michal Marek450bd1b2014-03-31 15:18:50 +02001334
Lucas De Marchi3bc92e82012-01-31 11:29:06 -02001335 else if (err < 0)
Lucas De Marchib1a51252012-01-29 01:49:09 -02001336 break;
1337 }
1338
1339 kmod_module_unref_list(list);
1340 return err;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001341}
1342
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001343/**
1344 * kmod_module_get_options:
1345 * @mod: kmod module
1346 *
1347 * Get options of this kmod module. Options come from the configuration file
1348 * and are cached in @mod. The first call to this function will search for
1349 * this module in configuration and subsequent calls return the cached string.
1350 *
1351 * Returns: a string with all the options separated by spaces. This string is
1352 * owned by @mod, do not free it.
1353 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001354KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1355{
1356 if (mod == NULL)
1357 return NULL;
1358
1359 if (!mod->init.options) {
1360 /* lazy init */
1361 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001362 const struct kmod_list *l;
1363 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001364 char *opts = NULL;
1365 size_t optslen = 0;
1366
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001367 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001368
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001369 kmod_list_foreach(l, config->options) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001370 const char *modname = kmod_option_get_modname(l);
1371 const char *str;
1372 size_t len;
1373 void *tmp;
1374
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001375 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1376 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1377 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001378 continue;
1379
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001380 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 -02001381 str = kmod_option_get_options(l);
1382 len = strlen(str);
1383 if (len < 1)
1384 continue;
1385
1386 tmp = realloc(opts, optslen + len + 2);
1387 if (tmp == NULL) {
1388 free(opts);
1389 goto failed;
1390 }
1391
1392 opts = tmp;
1393
1394 if (optslen > 0) {
1395 opts[optslen] = ' ';
1396 optslen++;
1397 }
1398
1399 memcpy(opts + optslen, str, len);
1400 optslen += len;
1401 opts[optslen] = '\0';
1402 }
1403
1404 m->init.options = true;
1405 m->options = opts;
1406 }
1407
1408 return mod->options;
1409
1410failed:
1411 ERR(mod->ctx, "out of memory\n");
1412 return NULL;
1413}
1414
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001415/**
1416 * kmod_module_get_install_commands:
1417 * @mod: kmod module
1418 *
1419 * Get install commands for this kmod module. Install commands come from the
1420 * configuration file and are cached in @mod. The first call to this function
1421 * will search for this module in configuration and subsequent calls return
1422 * the cached string. The install commands are returned as they were in the
1423 * configuration, concatenated by ';'. No other processing is made in this
1424 * string.
1425 *
1426 * Returns: a string with all install commands separated by semicolons. This
1427 * string is owned by @mod, do not free it.
1428 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001429KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1430{
1431 if (mod == NULL)
1432 return NULL;
1433
1434 if (!mod->init.install_commands) {
1435 /* lazy init */
1436 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001437 const struct kmod_list *l;
1438 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001439
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001440 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001441
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001442 kmod_list_foreach(l, config->install_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001443 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001444
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001445 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001446 continue;
1447
Lucas De Marchi60f67602011-12-16 03:33:26 -02001448 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001449
Lucas De Marchi60f67602011-12-16 03:33:26 -02001450 /*
1451 * find only the first command, as modprobe from
1452 * module-init-tools does
1453 */
1454 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001455 }
1456
1457 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001458 }
1459
1460 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001461}
1462
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001463void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1464{
1465 mod->init.install_commands = true;
1466 mod->install_commands = cmd;
1467}
1468
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001469static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1470{
1471 struct kmod_list *ret = NULL;
1472 unsigned i;
1473
1474 for (i = 0; i < count; i++) {
1475 const char *depname = array[i];
1476 struct kmod_list *lst = NULL;
1477 int err;
1478
1479 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1480 if (err < 0) {
1481 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1482 continue;
1483 } else if (lst != NULL)
1484 ret = kmod_list_append_list(ret, lst);
1485 }
1486 return ret;
1487}
1488
1489/**
1490 * kmod_module_get_softdeps:
1491 * @mod: kmod module
1492 * @pre: where to save the list of preceding soft dependencies.
1493 * @post: where to save the list of post soft dependencies.
1494 *
1495 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001496 * from configuration file and are not cached in @mod because it may include
1497 * dependency cycles that would make we leak kmod_module. Any call
1498 * to this function will search for this module in configuration, allocate a
1499 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001500 *
1501 * Both @pre and @post are newly created list of kmod_module and
1502 * should be unreferenced with kmod_module_unref_list().
1503 *
1504 * Returns: 0 on success or < 0 otherwise.
1505 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001506KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1507 struct kmod_list **pre,
1508 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001509{
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001510 const struct kmod_list *l;
1511 const struct kmod_config *config;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001512
1513 if (mod == NULL || pre == NULL || post == NULL)
1514 return -ENOENT;
1515
1516 assert(*pre == NULL);
1517 assert(*post == NULL);
1518
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001519 config = kmod_get_config(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001520
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001521 kmod_list_foreach(l, config->softdeps) {
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001522 const char *modname = kmod_softdep_get_name(l);
1523 const char * const *array;
1524 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001525
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001526 if (fnmatch(modname, mod->name, 0) != 0)
1527 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001528
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001529 array = kmod_softdep_get_pre(l, &count);
1530 *pre = lookup_softdep(mod->ctx, array, count);
1531 array = kmod_softdep_get_post(l, &count);
1532 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001533
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001534 /*
1535 * find only the first command, as modprobe from
1536 * module-init-tools does
1537 */
1538 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001539 }
1540
1541 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001542}
1543
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001544/**
1545 * kmod_module_get_remove_commands:
1546 * @mod: kmod module
1547 *
1548 * Get remove commands for this kmod module. Remove commands come from the
1549 * configuration file and are cached in @mod. The first call to this function
1550 * will search for this module in configuration and subsequent calls return
1551 * the cached string. The remove commands are returned as they were in the
1552 * configuration, concatenated by ';'. No other processing is made in this
1553 * string.
1554 *
1555 * Returns: a string with all remove commands separated by semicolons. This
1556 * string is owned by @mod, do not free it.
1557 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001558KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1559{
1560 if (mod == NULL)
1561 return NULL;
1562
1563 if (!mod->init.remove_commands) {
1564 /* lazy init */
1565 struct kmod_module *m = (struct kmod_module *)mod;
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001566 const struct kmod_list *l;
1567 const struct kmod_config *config;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001568
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001569 config = kmod_get_config(mod->ctx);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001570
Lucas De Marchie7fc2c82012-06-12 01:43:46 -03001571 kmod_list_foreach(l, config->remove_commands) {
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001572 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001573
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001574 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001575 continue;
1576
Lucas De Marchi60f67602011-12-16 03:33:26 -02001577 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001578
Lucas De Marchi60f67602011-12-16 03:33:26 -02001579 /*
1580 * find only the first command, as modprobe from
1581 * module-init-tools does
1582 */
1583 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001584 }
1585
1586 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001587 }
1588
1589 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001590}
1591
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001592void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1593{
1594 mod->init.remove_commands = true;
1595 mod->remove_commands = cmd;
1596}
1597
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001598/**
1599 * SECTION:libkmod-loaded
1600 * @short_description: currently loaded modules
1601 *
1602 * Information about currently loaded modules, as reported by Linux kernel.
1603 * These information are not cached by libkmod and are always read from /sys
1604 * and /proc/modules.
1605 */
1606
1607/**
1608 * kmod_module_new_from_loaded:
1609 * @ctx: kmod library context
1610 * @list: where to save the list of loaded modules
1611 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001612 * Create a new list of kmod modules with all modules currently loaded in
1613 * kernel. It uses /proc/modules to get the names of loaded modules and to
1614 * create kmod modules by calling kmod_module_new_from_name() in each of them.
Chengwei Yang491c4902013-05-04 17:07:02 +08001615 * They are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001616 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001617 * The initial refcount is 1, and needs to be decremented to release the
1618 * resources of the kmod_module. The returned @list must be released by
1619 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1620 * kmod_modules created, they are all released upon @ctx destruction too. Do
1621 * not unref @ctx before all the desired operations with the returned list are
1622 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001623 *
1624 * Returns: 0 on success or < 0 on error.
1625 */
1626KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1627 struct kmod_list **list)
1628{
1629 struct kmod_list *l = NULL;
1630 FILE *fp;
1631 char line[4096];
1632
1633 if (ctx == NULL || list == NULL)
1634 return -ENOENT;
1635
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001636 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001637 if (fp == NULL) {
1638 int err = -errno;
1639 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1640 return err;
1641 }
1642
1643 while (fgets(line, sizeof(line), fp)) {
1644 struct kmod_module *m;
1645 struct kmod_list *node;
1646 int err;
1647 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1648
1649 err = kmod_module_new_from_name(ctx, name, &m);
1650 if (err < 0) {
1651 ERR(ctx, "could not get module from name '%s': %s\n",
1652 name, strerror(-err));
1653 continue;
1654 }
1655
1656 node = kmod_list_append(l, m);
1657 if (node)
1658 l = node;
1659 else {
1660 ERR(ctx, "out of memory\n");
1661 kmod_module_unref(m);
1662 }
1663 }
1664
1665 fclose(fp);
1666 *list = l;
1667
1668 return 0;
1669}
1670
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001671/**
1672 * kmod_module_initstate_str:
1673 * @state: the state as returned by kmod_module_get_initstate()
1674 *
1675 * Translate a initstate to a string.
1676 *
1677 * Returns: the string associated to the @state. This string is statically
1678 * allocated, do not free it.
1679 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001680KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1681{
Dave Reisner7bede7b2012-02-02 15:05:57 -05001682 switch (state) {
1683 case KMOD_MODULE_BUILTIN:
1684 return "builtin";
1685 case KMOD_MODULE_LIVE:
1686 return "live";
1687 case KMOD_MODULE_COMING:
1688 return "coming";
1689 case KMOD_MODULE_GOING:
1690 return "going";
1691 default:
1692 return NULL;
1693 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001694}
1695
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001696/**
1697 * kmod_module_get_initstate:
1698 * @mod: kmod module
1699 *
1700 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1701 * /sys filesystem.
1702 *
Chengwei Yangd7152f62013-05-04 17:07:03 +08001703 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1704 * KMOD_MODULE_BUILTIN: module is builtin;
1705 * KMOD_MODULE_LIVE: module is live in kernel;
1706 * KMOD_MODULE_COMING: module is being loaded;
1707 * KMOD_MODULE_GOING: module is being unloaded.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001708 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001709KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1710{
1711 char path[PATH_MAX], buf[32];
1712 int fd, err, pathlen;
1713
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001714 if (mod == NULL)
1715 return -ENOENT;
1716
Lucas De Marchi38052742012-02-16 20:43:16 -02001717 if (mod->builtin)
1718 return KMOD_MODULE_BUILTIN;
1719
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001720 pathlen = snprintf(path, sizeof(path),
1721 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001722 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001723 if (fd < 0) {
1724 err = -errno;
1725
Lucas De Marchiddbda022011-12-27 11:40:10 -02001726 DBG(mod->ctx, "could not open '%s': %s\n",
1727 path, strerror(-err));
1728
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001729 if (pathlen > (int)sizeof("/initstate") - 1) {
1730 struct stat st;
1731 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1732 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1733 return KMOD_MODULE_BUILTIN;
1734 }
1735
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001736 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001737 path, strerror(-err));
1738 return err;
1739 }
1740
1741 err = read_str_safe(fd, buf, sizeof(buf));
1742 close(fd);
1743 if (err < 0) {
1744 ERR(mod->ctx, "could not read from '%s': %s\n",
1745 path, strerror(-err));
1746 return err;
1747 }
1748
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001749 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001750 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001751 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001752 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001753 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001754 return KMOD_MODULE_GOING;
1755
1756 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1757 return -EINVAL;
1758}
1759
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001760/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001761 * kmod_module_get_size:
1762 * @mod: kmod module
1763 *
Dave Reisner486f9012012-06-28 09:09:48 -04001764 * Get the size of this kmod module as returned by Linux kernel. If supported,
1765 * the size is read from the coresize attribute in /sys/module. For older
1766 * kernels, this falls back on /proc/modules and searches for the specified
1767 * module to get its size.
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001768 *
1769 * Returns: the size of this kmod module.
1770 */
1771KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1772{
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001773 FILE *fp;
1774 char line[4096];
1775 int lineno = 0;
1776 long size = -ENOENT;
Dave Reisner486f9012012-06-28 09:09:48 -04001777 int dfd, cfd;
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001778
1779 if (mod == NULL)
1780 return -ENOENT;
1781
Dave Reisner486f9012012-06-28 09:09:48 -04001782 /* try to open the module dir in /sys. If this fails, don't
1783 * bother trying to find the size as we know the module isn't
1784 * loaded.
1785 */
1786 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
Cristian Rodríguez74c26942014-06-18 20:51:00 -04001787 dfd = open(line, O_RDONLY|O_CLOEXEC);
Dave Reisner486f9012012-06-28 09:09:48 -04001788 if (dfd < 0)
1789 return -errno;
1790
1791 /* available as of linux 3.3.x */
1792 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1793 if (cfd >= 0) {
1794 if (read_str_long(cfd, &size, 10) < 0)
1795 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1796 close(cfd);
1797 goto done;
1798 }
1799
1800 /* fall back on parsing /proc/modules */
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001801 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001802 if (fp == NULL) {
1803 int err = -errno;
1804 ERR(mod->ctx,
1805 "could not open /proc/modules: %s\n", strerror(errno));
Leandro Pereira30bfd482014-04-28 21:04:48 -03001806 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001807 return err;
1808 }
1809
1810 while (fgets(line, sizeof(line), fp)) {
1811 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1812 long value;
1813
1814 lineno++;
1815 if (tok == NULL || !streq(tok, mod->name))
1816 continue;
1817
1818 tok = strtok_r(NULL, " \t", &saveptr);
1819 if (tok == NULL) {
1820 ERR(mod->ctx,
1821 "invalid line format at /proc/modules:%d\n", lineno);
1822 break;
1823 }
1824
1825 value = strtol(tok, &endptr, 10);
1826 if (endptr == tok || *endptr != '\0') {
1827 ERR(mod->ctx,
1828 "invalid line format at /proc/modules:%d\n", lineno);
1829 break;
1830 }
1831
1832 size = value;
1833 break;
1834 }
1835 fclose(fp);
Dave Reisner486f9012012-06-28 09:09:48 -04001836
1837done:
1838 close(dfd);
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001839 return size;
1840}
1841
1842/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001843 * kmod_module_get_refcnt:
1844 * @mod: kmod module
1845 *
1846 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1847 * /sys filesystem.
1848 *
1849 * Returns: 0 on success or < 0 on failure.
1850 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001851KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1852{
1853 char path[PATH_MAX];
1854 long refcnt;
1855 int fd, err;
1856
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001857 if (mod == NULL)
1858 return -ENOENT;
1859
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001860 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001861 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001862 if (fd < 0) {
1863 err = -errno;
Lucas De Marchi9c5f0572012-03-01 14:04:29 -03001864 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001865 path, strerror(errno));
1866 return err;
1867 }
1868
1869 err = read_str_long(fd, &refcnt, 10);
1870 close(fd);
1871 if (err < 0) {
1872 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1873 path, strerror(-err));
1874 return err;
1875 }
1876
1877 return (int)refcnt;
1878}
1879
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001880/**
1881 * kmod_module_get_holders:
1882 * @mod: kmod module
1883 *
1884 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1885 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1886 *
1887 * Returns: a new list of kmod modules on success or NULL on failure.
1888 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001889KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1890{
1891 char dname[PATH_MAX];
1892 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001893 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001894 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001895
Lucas De Marchib9a7da32013-04-23 20:47:28 -03001896 if (mod == NULL || mod->ctx == NULL)
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001897 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001898
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001899 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1900
1901 d = opendir(dname);
1902 if (d == NULL) {
1903 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001904 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001905 return NULL;
1906 }
1907
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001908 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001909 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001910 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001911 int err;
1912
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001913 if (dent->d_name[0] == '.') {
1914 if (dent->d_name[1] == '\0' ||
1915 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001916 continue;
1917 }
1918
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001919 err = kmod_module_new_from_name(mod->ctx, dent->d_name,
1920 &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001921 if (err < 0) {
1922 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001923 dent->d_name, strerror(-err));
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001924 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001925 }
1926
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001927 l = kmod_list_append(list, holder);
1928 if (l != NULL) {
1929 list = l;
1930 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001931 ERR(mod->ctx, "out of memory\n");
1932 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001933 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001934 }
1935 }
1936
1937 closedir(d);
1938 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001939
1940fail:
1941 closedir(d);
1942 kmod_module_unref_list(list);
1943 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001944}
1945
1946struct kmod_module_section {
1947 unsigned long address;
1948 char name[];
1949};
1950
1951static void kmod_module_section_free(struct kmod_module_section *section)
1952{
1953 free(section);
1954}
1955
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001956/**
1957 * kmod_module_get_sections:
1958 * @mod: kmod module
1959 *
1960 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1961 * structure contained in this list is internal to libkmod and their fields
1962 * can be obtained by calling kmod_module_section_get_name() and
1963 * kmod_module_section_get_address().
1964 *
1965 * After use, free the @list by calling kmod_module_section_free_list().
1966 *
1967 * Returns: a new list of kmod module sections on success or NULL on failure.
1968 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001969KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1970{
1971 char dname[PATH_MAX];
1972 struct kmod_list *list = NULL;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001973 struct dirent *dent;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001974 DIR *d;
1975 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001976
1977 if (mod == NULL)
1978 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001979
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001980 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1981
1982 d = opendir(dname);
1983 if (d == NULL) {
1984 ERR(mod->ctx, "could not open '%s': %s\n",
1985 dname, strerror(errno));
1986 return NULL;
1987 }
1988
1989 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001990
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001991 for (dent = readdir(d); dent; dent = readdir(d)) {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001992 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001993 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001994 unsigned long address;
1995 size_t namesz;
1996 int fd, err;
1997
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03001998 if (dent->d_name[0] == '.') {
1999 if (dent->d_name[1] == '\0' ||
2000 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002001 continue;
2002 }
2003
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002004 fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002005 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002006 ERR(mod->ctx, "could not open '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002007 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002008 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002009 }
2010
2011 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002012 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002013 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002014 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002015 dname, dent->d_name);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002016 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002017 }
2018
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002019 namesz = strlen(dent->d_name) + 1;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002020 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002021
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002022 if (section == NULL) {
2023 ERR(mod->ctx, "out of memory\n");
2024 goto fail;
2025 }
2026
2027 section->address = address;
Lucas De Marchi7e0385c2013-08-27 23:29:47 -03002028 memcpy(section->name, dent->d_name, namesz);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002029
2030 l = kmod_list_append(list, section);
2031 if (l != NULL) {
2032 list = l;
2033 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002034 ERR(mod->ctx, "out of memory\n");
2035 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002036 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002037 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002038 }
2039
2040 closedir(d);
2041 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02002042
2043fail:
2044 closedir(d);
2045 kmod_module_unref_list(list);
2046 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002047}
2048
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002049/**
2050 * kmod_module_section_get_module_name:
2051 * @entry: a list entry representing a kmod module section
2052 *
2053 * Get the name of a kmod module section.
2054 *
2055 * After use, free the @list by calling kmod_module_section_free_list().
2056 *
2057 * Returns: the name of this kmod module section on success or NULL on
2058 * failure. The string is owned by the section, do not free it.
2059 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002060KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2061{
2062 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002063
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002064 if (entry == NULL)
2065 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002066
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002067 section = entry->data;
2068 return section->name;
2069}
2070
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002071/**
2072 * kmod_module_section_get_address:
2073 * @entry: a list entry representing a kmod module section
2074 *
2075 * Get the address of a kmod module section.
2076 *
2077 * After use, free the @list by calling kmod_module_section_free_list().
2078 *
2079 * Returns: the address of this kmod module section on success or ULONG_MAX
2080 * on failure.
2081 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002082KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2083{
2084 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002085
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002086 if (entry == NULL)
2087 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02002088
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002089 section = entry->data;
2090 return section->address;
2091}
2092
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02002093/**
2094 * kmod_module_section_free_list:
2095 * @list: kmod module section list
2096 *
2097 * Release the resources taken by @list
2098 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02002099KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2100{
2101 while (list) {
2102 kmod_module_section_free(list->data);
2103 list = kmod_list_remove(list);
2104 }
2105}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002106
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002107static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2108{
2109 if (mod->file == NULL) {
2110 const char *path = kmod_module_get_path(mod);
2111
2112 if (path == NULL) {
2113 errno = ENOENT;
2114 return NULL;
2115 }
2116
2117 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2118 path);
2119 if (mod->file == NULL)
2120 return NULL;
2121 }
2122
2123 return kmod_file_get_elf(mod->file);
2124}
2125
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002126struct kmod_module_info {
2127 char *key;
2128 char value[];
2129};
2130
2131static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2132{
2133 struct kmod_module_info *info;
2134
2135 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2136 if (info == NULL)
2137 return NULL;
2138
2139 info->key = (char *)info + sizeof(struct kmod_module_info)
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002140 + valuelen + 1;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002141 memcpy(info->key, key, keylen);
2142 info->key[keylen] = '\0';
2143 memcpy(info->value, value, valuelen);
2144 info->value[valuelen] = '\0';
2145 return info;
2146}
2147
2148static void kmod_module_info_free(struct kmod_module_info *info)
2149{
2150 free(info);
2151}
2152
Michal Marekf64458c2013-01-16 10:18:17 +01002153static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2154{
2155 struct kmod_module_info *info;
2156 struct kmod_list *n;
2157
2158 info = kmod_module_info_new(key, keylen, value, valuelen);
Michal Marek63339342013-01-16 17:51:57 +01002159 if (info == NULL)
Michal Marekf64458c2013-01-16 10:18:17 +01002160 return NULL;
Michal Marekf64458c2013-01-16 10:18:17 +01002161 n = kmod_list_append(*list, info);
Michal Marek63339342013-01-16 17:51:57 +01002162 if (n != NULL)
2163 *list = n;
2164 else
Michal Marekf64458c2013-01-16 10:18:17 +01002165 kmod_module_info_free(info);
Michal Marekf64458c2013-01-16 10:18:17 +01002166 return n;
2167}
2168
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002169/**
2170 * kmod_module_get_info:
2171 * @mod: kmod module
2172 * @list: where to return list of module information. Use
2173 * kmod_module_info_get_key() and
2174 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002175 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002176 *
2177 * Get a list of entries in ELF section ".modinfo", these contain
2178 * alias, license, depends, vermagic and other keys with respective
Michal Marek8fe16812013-01-16 09:52:01 +01002179 * values. If the module is signed (CONFIG_MODULE_SIG), information
2180 * about the module signature is included as well: signer,
2181 * sig_key and sig_hashalgo.
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002182 *
2183 * After use, free the @list by calling kmod_module_info_free_list().
2184 *
2185 * Returns: 0 on success or < 0 otherwise.
2186 */
2187KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2188{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002189 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002190 char **strings;
Michal Marekf64458c2013-01-16 10:18:17 +01002191 int i, count, ret = -ENOMEM;
Michal Marek8fe16812013-01-16 09:52:01 +01002192 struct kmod_signature_info sig_info;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002193
2194 if (mod == NULL || list == NULL)
2195 return -ENOENT;
2196
2197 assert(*list == NULL);
2198
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002199 elf = kmod_module_get_elf(mod);
2200 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002201 return -errno;
2202
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002203 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002204 if (count < 0)
2205 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002206
2207 for (i = 0; i < count; i++) {
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002208 struct kmod_list *n;
2209 const char *key, *value;
2210 size_t keylen, valuelen;
2211
2212 key = strings[i];
2213 value = strchr(key, '=');
2214 if (value == NULL) {
2215 keylen = strlen(key);
2216 valuelen = 0;
Lucas De Marchi818af4f2013-04-23 20:33:13 -03002217 value = key;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002218 } else {
2219 keylen = value - key;
2220 value++;
2221 valuelen = strlen(value);
2222 }
2223
Michal Marekf64458c2013-01-16 10:18:17 +01002224 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2225 if (n == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002226 goto list_error;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002227 }
Michal Marek8fe16812013-01-16 09:52:01 +01002228
2229 if (kmod_module_signature_info(mod->file, &sig_info)) {
2230 struct kmod_list *n;
2231 char *key_hex;
2232
2233 n = kmod_module_info_append(list, "signer", strlen("signer"),
2234 sig_info.signer, sig_info.signer_len);
2235 if (n == NULL)
2236 goto list_error;
2237 count++;
2238
2239 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2240 key_hex = malloc(sig_info.key_id_len * 3);
2241 if (key_hex == NULL)
2242 goto list_error;
2243 for (i = 0; i < (int)sig_info.key_id_len; i++) {
2244 sprintf(key_hex + i * 3, "%02X",
2245 (unsigned char)sig_info.key_id[i]);
2246 if (i < (int)sig_info.key_id_len - 1)
2247 key_hex[i * 3 + 2] = ':';
2248 }
2249 n = kmod_module_info_append(list, "sig_key", strlen("sig_key"),
2250 key_hex, sig_info.key_id_len * 3 - 1);
2251 free(key_hex);
2252 if (n == NULL)
2253 goto list_error;
2254 count++;
2255
2256 n = kmod_module_info_append(list,
2257 "sig_hashalgo", strlen("sig_hashalgo"),
2258 sig_info.hash_algo, strlen(sig_info.hash_algo));
2259 if (n == NULL)
2260 goto list_error;
2261 count++;
2262
2263 /*
2264 * Omit sig_info.id_type and sig_info.algo for now, as these
2265 * are currently constant.
2266 */
2267 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002268 ret = count;
2269
2270list_error:
Michal Marek63339342013-01-16 17:51:57 +01002271 if (ret < 0) {
2272 kmod_module_info_free_list(*list);
2273 *list = NULL;
2274 }
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002275 free(strings);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002276 return ret;
2277}
2278
2279/**
2280 * kmod_module_info_get_key:
2281 * @entry: a list entry representing a kmod module info
2282 *
2283 * Get the key of a kmod module info.
2284 *
2285 * Returns: the key of this kmod module info on success or NULL on
2286 * failure. The string is owned by the info, do not free it.
2287 */
2288KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2289{
2290 struct kmod_module_info *info;
2291
2292 if (entry == NULL)
2293 return NULL;
2294
2295 info = entry->data;
2296 return info->key;
2297}
2298
2299/**
2300 * kmod_module_info_get_value:
2301 * @entry: a list entry representing a kmod module info
2302 *
2303 * Get the value of a kmod module info.
2304 *
2305 * Returns: the value of this kmod module info on success or NULL on
2306 * failure. The string is owned by the info, do not free it.
2307 */
2308KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2309{
2310 struct kmod_module_info *info;
2311
2312 if (entry == NULL)
2313 return NULL;
2314
2315 info = entry->data;
2316 return info->value;
2317}
2318
2319/**
2320 * kmod_module_info_free_list:
2321 * @list: kmod module info list
2322 *
2323 * Release the resources taken by @list
2324 */
2325KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2326{
2327 while (list) {
2328 kmod_module_info_free(list->data);
2329 list = kmod_list_remove(list);
2330 }
2331}
2332
2333struct kmod_module_version {
2334 uint64_t crc;
2335 char symbol[];
2336};
2337
2338static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2339{
2340 struct kmod_module_version *mv;
2341 size_t symbollen = strlen(symbol) + 1;
2342
2343 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2344 if (mv == NULL)
2345 return NULL;
2346
2347 mv->crc = crc;
2348 memcpy(mv->symbol, symbol, symbollen);
2349 return mv;
2350}
2351
2352static void kmod_module_version_free(struct kmod_module_version *version)
2353{
2354 free(version);
2355}
2356
2357/**
2358 * kmod_module_get_versions:
2359 * @mod: kmod module
2360 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002361 * kmod_module_version_get_symbol() and
2362 * kmod_module_version_get_crc(). Release this list with
2363 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002364 *
2365 * Get a list of entries in ELF section "__versions".
2366 *
2367 * After use, free the @list by calling kmod_module_versions_free_list().
2368 *
2369 * Returns: 0 on success or < 0 otherwise.
2370 */
2371KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2372{
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002373 struct kmod_elf *elf;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002374 struct kmod_modversion *versions;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002375 int i, count, ret = 0;
2376
2377 if (mod == NULL || list == NULL)
2378 return -ENOENT;
2379
2380 assert(*list == NULL);
2381
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002382 elf = kmod_module_get_elf(mod);
2383 if (elf == NULL)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002384 return -errno;
2385
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002386 count = kmod_elf_get_modversions(elf, &versions);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002387 if (count < 0)
2388 return count;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002389
2390 for (i = 0; i < count; i++) {
2391 struct kmod_module_version *mv;
2392 struct kmod_list *n;
2393
2394 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2395 if (mv == NULL) {
2396 ret = -errno;
2397 kmod_module_versions_free_list(*list);
2398 *list = NULL;
2399 goto list_error;
2400 }
2401
2402 n = kmod_list_append(*list, mv);
2403 if (n != NULL)
2404 *list = n;
2405 else {
2406 kmod_module_version_free(mv);
2407 kmod_module_versions_free_list(*list);
2408 *list = NULL;
2409 ret = -ENOMEM;
2410 goto list_error;
2411 }
2412 }
2413 ret = count;
2414
2415list_error:
2416 free(versions);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002417 return ret;
2418}
2419
2420/**
Chengwei Yang491c4902013-05-04 17:07:02 +08002421 * kmod_module_version_get_symbol:
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002422 * @entry: a list entry representing a kmod module versions
2423 *
2424 * Get the symbol of a kmod module versions.
2425 *
2426 * Returns: the symbol of this kmod module versions on success or NULL
2427 * on failure. The string is owned by the versions, do not free it.
2428 */
2429KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2430{
2431 struct kmod_module_version *version;
2432
2433 if (entry == NULL)
2434 return NULL;
2435
2436 version = entry->data;
2437 return version->symbol;
2438}
2439
2440/**
2441 * kmod_module_version_get_crc:
2442 * @entry: a list entry representing a kmod module version
2443 *
2444 * Get the crc of a kmod module version.
2445 *
2446 * Returns: the crc of this kmod module version on success or NULL on
2447 * failure. The string is owned by the version, do not free it.
2448 */
2449KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2450{
2451 struct kmod_module_version *version;
2452
2453 if (entry == NULL)
2454 return 0;
2455
2456 version = entry->data;
2457 return version->crc;
2458}
2459
2460/**
2461 * kmod_module_versions_free_list:
2462 * @list: kmod module versions list
2463 *
2464 * Release the resources taken by @list
2465 */
2466KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2467{
2468 while (list) {
2469 kmod_module_version_free(list->data);
2470 list = kmod_list_remove(list);
2471 }
2472}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002473
2474struct kmod_module_symbol {
2475 uint64_t crc;
2476 char symbol[];
2477};
2478
2479static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2480{
2481 struct kmod_module_symbol *mv;
2482 size_t symbollen = strlen(symbol) + 1;
2483
2484 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2485 if (mv == NULL)
2486 return NULL;
2487
2488 mv->crc = crc;
2489 memcpy(mv->symbol, symbol, symbollen);
2490 return mv;
2491}
2492
2493static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2494{
2495 free(symbol);
2496}
2497
2498/**
2499 * kmod_module_get_symbols:
2500 * @mod: kmod module
2501 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002502 * kmod_module_symbol_get_symbol() and
2503 * kmod_module_symbol_get_crc(). Release this list with
2504 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002505 *
2506 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2507 *
2508 * After use, free the @list by calling kmod_module_symbols_free_list().
2509 *
2510 * Returns: 0 on success or < 0 otherwise.
2511 */
2512KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2513{
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002514 struct kmod_elf *elf;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002515 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002516 int i, count, ret = 0;
2517
2518 if (mod == NULL || list == NULL)
2519 return -ENOENT;
2520
2521 assert(*list == NULL);
2522
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002523 elf = kmod_module_get_elf(mod);
2524 if (elf == NULL)
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002525 return -errno;
2526
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002527 count = kmod_elf_get_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002528 if (count < 0)
2529 return count;
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002530
2531 for (i = 0; i < count; i++) {
2532 struct kmod_module_symbol *mv;
2533 struct kmod_list *n;
2534
2535 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2536 if (mv == NULL) {
2537 ret = -errno;
2538 kmod_module_symbols_free_list(*list);
2539 *list = NULL;
2540 goto list_error;
2541 }
2542
2543 n = kmod_list_append(*list, mv);
2544 if (n != NULL)
2545 *list = n;
2546 else {
2547 kmod_module_symbol_free(mv);
2548 kmod_module_symbols_free_list(*list);
2549 *list = NULL;
2550 ret = -ENOMEM;
2551 goto list_error;
2552 }
2553 }
2554 ret = count;
2555
2556list_error:
2557 free(symbols);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002558 return ret;
2559}
2560
2561/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002562 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002563 * @entry: a list entry representing a kmod module symbols
2564 *
2565 * Get the symbol of a kmod module symbols.
2566 *
2567 * Returns: the symbol of this kmod module symbols on success or NULL
2568 * on failure. The string is owned by the symbols, do not free it.
2569 */
2570KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2571{
2572 struct kmod_module_symbol *symbol;
2573
2574 if (entry == NULL)
2575 return NULL;
2576
2577 symbol = entry->data;
2578 return symbol->symbol;
2579}
2580
2581/**
2582 * kmod_module_symbol_get_crc:
2583 * @entry: a list entry representing a kmod module symbol
2584 *
2585 * Get the crc of a kmod module symbol.
2586 *
2587 * Returns: the crc of this kmod module symbol on success or NULL on
2588 * failure. The string is owned by the symbol, do not free it.
2589 */
2590KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2591{
2592 struct kmod_module_symbol *symbol;
2593
2594 if (entry == NULL)
2595 return 0;
2596
2597 symbol = entry->data;
2598 return symbol->crc;
2599}
2600
2601/**
2602 * kmod_module_symbols_free_list:
2603 * @list: kmod module symbols list
2604 *
2605 * Release the resources taken by @list
2606 */
2607KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2608{
2609 while (list) {
2610 kmod_module_symbol_free(list->data);
2611 list = kmod_list_remove(list);
2612 }
2613}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002614
2615struct kmod_module_dependency_symbol {
2616 uint64_t crc;
2617 uint8_t bind;
2618 char symbol[];
2619};
2620
2621static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2622{
2623 struct kmod_module_dependency_symbol *mv;
2624 size_t symbollen = strlen(symbol) + 1;
2625
2626 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2627 if (mv == NULL)
2628 return NULL;
2629
2630 mv->crc = crc;
2631 mv->bind = bind;
2632 memcpy(mv->symbol, symbol, symbollen);
2633 return mv;
2634}
2635
2636static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2637{
2638 free(dependency_symbol);
2639}
2640
2641/**
2642 * kmod_module_get_dependency_symbols:
2643 * @mod: kmod module
2644 * @list: where to return list of module dependency_symbols. Use
2645 * kmod_module_dependency_symbol_get_symbol() and
2646 * kmod_module_dependency_symbol_get_crc(). Release this list with
2647 * kmod_module_dependency_symbols_free_list()
2648 *
2649 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2650 *
2651 * After use, free the @list by calling
2652 * kmod_module_dependency_symbols_free_list().
2653 *
2654 * Returns: 0 on success or < 0 otherwise.
2655 */
2656KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2657{
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002658 struct kmod_elf *elf;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002659 struct kmod_modversion *symbols;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002660 int i, count, ret = 0;
2661
2662 if (mod == NULL || list == NULL)
2663 return -ENOENT;
2664
2665 assert(*list == NULL);
2666
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002667 elf = kmod_module_get_elf(mod);
2668 if (elf == NULL)
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002669 return -errno;
2670
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002671 count = kmod_elf_get_dependency_symbols(elf, &symbols);
Lucas De Marchi1eff9422012-10-18 01:36:33 -03002672 if (count < 0)
2673 return count;
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002674
2675 for (i = 0; i < count; i++) {
2676 struct kmod_module_dependency_symbol *mv;
2677 struct kmod_list *n;
2678
2679 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2680 symbols[i].bind,
2681 symbols[i].symbol);
2682 if (mv == NULL) {
2683 ret = -errno;
2684 kmod_module_dependency_symbols_free_list(*list);
2685 *list = NULL;
2686 goto list_error;
2687 }
2688
2689 n = kmod_list_append(*list, mv);
2690 if (n != NULL)
2691 *list = n;
2692 else {
2693 kmod_module_dependency_symbol_free(mv);
2694 kmod_module_dependency_symbols_free_list(*list);
2695 *list = NULL;
2696 ret = -ENOMEM;
2697 goto list_error;
2698 }
2699 }
2700 ret = count;
2701
2702list_error:
2703 free(symbols);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002704 return ret;
2705}
2706
2707/**
2708 * kmod_module_dependency_symbol_get_symbol:
2709 * @entry: a list entry representing a kmod module dependency_symbols
2710 *
2711 * Get the dependency symbol of a kmod module
2712 *
2713 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2714 * on failure. The string is owned by the dependency_symbols, do not free it.
2715 */
2716KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2717{
2718 struct kmod_module_dependency_symbol *dependency_symbol;
2719
2720 if (entry == NULL)
2721 return NULL;
2722
2723 dependency_symbol = entry->data;
2724 return dependency_symbol->symbol;
2725}
2726
2727/**
2728 * kmod_module_dependency_symbol_get_crc:
2729 * @entry: a list entry representing a kmod module dependency_symbol
2730 *
2731 * Get the crc of a kmod module dependency_symbol.
2732 *
2733 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2734 * failure. The string is owned by the dependency_symbol, do not free it.
2735 */
2736KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2737{
2738 struct kmod_module_dependency_symbol *dependency_symbol;
2739
2740 if (entry == NULL)
2741 return 0;
2742
2743 dependency_symbol = entry->data;
2744 return dependency_symbol->crc;
2745}
2746
2747/**
2748 * kmod_module_dependency_symbol_get_bind:
2749 * @entry: a list entry representing a kmod module dependency_symbol
2750 *
2751 * Get the bind type of a kmod module dependency_symbol.
2752 *
2753 * Returns: the bind of this kmod module dependency_symbol on success
2754 * or < 0 on failure.
2755 */
2756KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2757{
2758 struct kmod_module_dependency_symbol *dependency_symbol;
2759
2760 if (entry == NULL)
2761 return 0;
2762
2763 dependency_symbol = entry->data;
2764 return dependency_symbol->bind;
2765}
2766
2767/**
2768 * kmod_module_dependency_symbols_free_list:
2769 * @list: kmod module dependency_symbols list
2770 *
2771 * Release the resources taken by @list
2772 */
2773KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2774{
2775 while (list) {
2776 kmod_module_dependency_symbol_free(list->data);
2777 list = kmod_list_remove(list);
2778 }
2779}