blob: 47b1709f01cdf1e4ffe3c3df60751d2dfc3b21a7 [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
Lucas De Marchia66a6a92012-01-09 00:40:50 -02004 * Copyright (C) 2011-2012 ProFUSION embedded systems
Lucas De Marchi8f788d52011-11-25 01:22:56 -02005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
Lucas De Marchicb451f32011-12-12 18:24:35 -02008 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchi8f788d52011-11-25 01:22:56 -020010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Lucas De Marchi7636e722011-12-01 17:56:03 -020021#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020022#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <stdarg.h>
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <ctype.h>
30#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020031#include <limits.h>
32#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020033#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/mman.h>
36#include <string.h>
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020037#include <fnmatch.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020038
39#include "libkmod.h"
40#include "libkmod-private.h"
41
42/**
Lucas De Marchi66819512012-01-09 04:47:40 -020043 * SECTION:libkmod-module
44 * @short_description: operate on kernel modules
45 */
46
47/**
Lucas De Marchi8f788d52011-11-25 01:22:56 -020048 * kmod_module:
49 *
50 * Opaque object representing a module.
51 */
52struct kmod_module {
53 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020054 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020055 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020056 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020057 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020058 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020059 const char *install_commands; /* owned by kmod_config */
60 const char *remove_commands; /* owned by kmod_config */
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020061 char *alias; /* only set if this module was created from an alias */
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020062 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020063 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020064 struct {
65 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020066 bool options : 1;
67 bool install_commands : 1;
68 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020069 } init;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020070};
71
Lucas De Marchic35347f2011-12-12 10:48:02 -020072static inline const char *path_join(const char *path, size_t prefixlen,
73 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020074{
75 size_t pathlen;
76
77 if (path[0] == '/')
78 return path;
79
80 pathlen = strlen(path);
81 if (prefixlen + pathlen + 1 >= PATH_MAX)
82 return NULL;
83
84 memcpy(buf + prefixlen, path, pathlen + 1);
85 return buf;
86}
87
Lucas De Marchi671d4892011-12-05 20:23:05 -020088int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -020089{
90 struct kmod_ctx *ctx = mod->ctx;
91 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020092 const char *dirname;
93 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -020094 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -020095 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020096 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -020097
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020098 if (mod->init.dep)
99 return mod->n_dep;
100 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200101 mod->init.dep = true;
102
103 p = strchr(line, ':');
104 if (p == NULL)
105 return 0;
106
Lucas De Marchi671d4892011-12-05 20:23:05 -0200107 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200108 dirname = kmod_get_dirname(mod->ctx);
109 dirnamelen = strlen(dirname);
110 if (dirnamelen + 2 >= PATH_MAX)
111 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200112
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200113 memcpy(buf, dirname, dirnamelen);
114 buf[dirnamelen] = '/';
115 dirnamelen++;
116 buf[dirnamelen] = '\0';
117
118 if (mod->path == NULL) {
119 const char *str = path_join(line, dirnamelen, buf);
120 if (str == NULL)
121 return 0;
122 mod->path = strdup(str);
123 if (mod->path == NULL)
124 return 0;
125 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200126
Lucas De Marchi7636e722011-12-01 17:56:03 -0200127 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200128 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
129 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200130 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200131 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200132
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200133 path = path_join(p, dirnamelen, buf);
134 if (path == NULL) {
135 ERR(ctx, "could not join path '%s' and '%s'.\n",
136 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200137 goto fail;
138 }
139
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200140 err = kmod_module_new_from_path(ctx, path, &depmod);
141 if (err < 0) {
142 ERR(ctx, "ctx=%p path=%s error=%s\n",
143 ctx, path, strerror(-err));
144 goto fail;
145 }
146
147 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200148
Lucas De Marchib94a7372011-12-26 20:10:49 -0200149 list = kmod_list_prepend(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200150 n++;
151 }
152
153 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
154
155 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200156 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200157 return n;
158
159fail:
160 kmod_module_unref_list(list);
161 mod->init.dep = false;
162 return err;
163}
164
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200165/**
166 * kmod_module_new_from_name:
167 * @ctx: kmod library context
168 * @name: name of the module
169 * @mod: where to save the created struct kmod_module
170 *
171 * Create a new struct kmod_module using the module name. @name can not be an
172 * alias, file name or anything else; it must be a module name. There's no
173 * check if the module does exists in the system.
174 *
175 * This function is also used internally by many others that return a new
176 * struct kmod_module or a new list of modules.
177 *
178 * The initial refcount is 1, and needs to be decremented to release the
179 * resources of the kmod_module. Since libkmod keeps track of all
180 * kmod_modules created, they are all released upon @ctx destruction too. Do
181 * not unref @ctx before all the desired operations with the returned
182 * kmod_module are done.
183 *
184 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
185 * module name or if memory allocation failed.
186 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200187KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
188 const char *name,
189 struct kmod_module **mod)
190{
191 struct kmod_module *m;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200192 size_t namelen;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200193 char name_norm[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200194 char *namesep;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200195
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200196 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200197 return -ENOENT;
198
Luis Felipe Strano Moraes9efaf2f2011-12-20 08:13:56 -0800199 if (alias_normalize(name, name_norm, &namelen) < 0) {
200 DBG(ctx, "invalid alias: %s\n", name);
201 return -EINVAL;
202 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200203
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200204 m = kmod_pool_get_module(ctx, name_norm);
205 if (m != NULL) {
206 *mod = kmod_module_ref(m);
207 return 0;
208 }
209
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200210 namesep = strchr(name_norm, '/');
211 m = malloc(sizeof(*m) + (namesep == NULL ? 1 : 2) * namelen + 2);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200212 if (m == NULL) {
213 free(m);
214 return -ENOMEM;
215 }
216
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200217 memset(m, 0, sizeof(*m));
218
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200219 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200220 m->name = (char *)m + sizeof(*m);
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200221 memcpy(m->name, name_norm, namelen + 1);
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200222
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200223 if (namesep) {
224 size_t len = namesep - name_norm;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200225
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200226 m->name[len] = '\0';
227 m->alias = m->name + len + 1;
228 m->hashkey = m->name + namelen + 1;
229 memcpy(m->hashkey, name_norm, namelen + 1);
230 } else {
231 m->hashkey = m->name;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200232 }
233
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200234 m->refcount = 1;
235 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200236 *mod = m;
237
238 return 0;
239}
240
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200241int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
242 const char *name, struct kmod_module **mod)
243{
244 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200245 char key[PATH_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200246 size_t namelen = strlen(name);
247 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200248
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200249 if (namelen + aliaslen + 2 > PATH_MAX)
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200250 return -ENAMETOOLONG;
251
252 memcpy(key, name, namelen);
253 memcpy(key + namelen + 1, alias, aliaslen + 1);
254 key[namelen] = '/';
255
256 err = kmod_module_new_from_name(ctx, key, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200257 if (err < 0)
258 return err;
259
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200260 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200261}
262
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200263/**
264 * kmod_module_new_from_path:
265 * @ctx: kmod library context
266 * @path: path where to find the given module
267 * @mod: where to save the created struct kmod_module
268 *
269 * Create a new struct kmod_module using the module path. @path must be an
270 * existent file with in the filesystem and must be accessible to libkmod.
271 *
272 * The initial refcount is 1, and needs to be decremented to release the
273 * resources of the kmod_module. Since libkmod keeps track of all
274 * kmod_modules created, they are all released upon @ctx destruction too. Do
275 * not unref @ctx before all the desired operations with the returned
276 * kmod_module are done.
277 *
278 * If @path is relative, it's treated as relative to the current working
279 * directory. Otherwise, give an absolute path.
280 *
281 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
282 * it's not a valid file for a kmod_module or if memory allocation failed.
283 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200284KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
285 const char *path,
286 struct kmod_module **mod)
287{
288 struct kmod_module *m;
289 int err;
290 struct stat st;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200291 char name[PATH_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200292 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200293 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200294
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200295 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200296 return -ENOENT;
297
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200298 abspath = path_make_absolute_cwd(path);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200299 if (abspath == NULL) {
300 DBG(ctx, "no absolute path for %s\n", path);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200301 return -ENOMEM;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200302 }
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200303
304 err = stat(abspath, &st);
305 if (err < 0) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200306 err = -errno;
307 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200308 free(abspath);
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200309 return err;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200310 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200311
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200312 if (path_to_modname(path, name, &namelen) == NULL) {
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200313 DBG(ctx, "could not get modname from path %s\n", path);
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200314 free(abspath);
315 return -ENOENT;
316 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200317
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200318 m = kmod_pool_get_module(ctx, name);
319 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200320 if (m->path == NULL)
321 m->path = abspath;
322 else if (streq(m->path, abspath))
323 free(abspath);
324 else {
Lucas De Marchiebaa7be2011-12-27 18:10:19 -0200325 ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
326 name, abspath, m->path);
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200327 free(abspath);
328 return -EEXIST;
329 }
330
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200331 *mod = kmod_module_ref(m);
332 return 0;
333 }
334
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200335 m = malloc(sizeof(*m) + namelen + 1);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200336 if (m == NULL)
337 return -errno;
338
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200339 memset(m, 0, sizeof(*m));
340
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200341 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200342 m->name = (char *)m + sizeof(*m);
Lucas De Marchi9dec2442011-12-18 15:12:19 -0200343 memcpy(m->name, name, namelen + 1);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200344 m->path = abspath;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200345 m->hashkey = m->name;
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200346 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200347
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200348 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200349
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200350 *mod = m;
351
352 return 0;
353}
354
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200355/**
356 * kmod_module_unref:
357 * @mod: kmod module
358 *
359 * Drop a reference of the kmod module. If the refcount reaches zero, its
360 * resources are released.
361 *
362 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
363 * returns the passed @mod with its refcount decremented.
364 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200365KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
366{
367 if (mod == NULL)
368 return NULL;
369
370 if (--mod->refcount > 0)
371 return mod;
372
373 DBG(mod->ctx, "kmod_module %p released\n", mod);
374
Lucas De Marchi4084c172011-12-15 13:43:22 -0200375 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200376 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200377 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200378 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200379 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200380 free(mod);
381 return NULL;
382}
383
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200384/**
385 * kmod_module_ref:
386 * @mod: kmod module
387 *
388 * Take a reference of the kmod module, incrementing its refcount.
389 *
390 * Returns: the passed @module with its refcount incremented.
391 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200392KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
393{
394 if (mod == NULL)
395 return NULL;
396
397 mod->refcount++;
398
399 return mod;
400}
401
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200402#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
403 do { \
404 if ((_err) < 0) \
405 goto _label_err; \
406 if (*(_list) != NULL) \
407 goto finish; \
408 } while (0)
409
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200410/**
411 * kmod_module_new_from_lookup:
412 * @ctx: kmod library context
413 * @given_alias: alias to look for
414 * @list: an empty list where to save the list of modules matching
415 * @given_alias
416 *
417 * Create a new list of kmod modules using an alias or module name and lookup
418 * libkmod's configuration files and indexes in order to find the module.
419 * Once it's found in one of the places, it stops searching and create the
420 * list of modules that is saved in @list.
421 *
422 * The search order is: 1. aliases in configuration file; 2. module names in
423 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
424 * in modules.alias index.
425 *
426 * The initial refcount is 1, and needs to be decremented to release the
427 * resources of the kmod_module. The returned @list must be released by
428 * calling kmod_module_unref_list(). Since libkmod keeps track of all
429 * kmod_modules created, they are all released upon @ctx destruction too. Do
430 * not unref @ctx before all the desired operations with the returned list are
431 * completed.
432 *
433 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
434 * methods failed, which is basically due to memory allocation fail. If module
435 * is not found, it still returns 0, but @list is an empty list.
436 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200437KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200438 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200439 struct kmod_list **list)
440{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200441 int err;
Lucas De Marchi6daceb22012-01-08 01:02:29 -0200442 char alias[PATH_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200443
Lucas De Marchi4308b172011-12-13 10:26:04 -0200444 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200445 return -ENOENT;
446
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200447 if (list == NULL || *list != NULL) {
448 ERR(ctx, "An empty list is needed to create lookup\n");
449 return -ENOSYS;
450 }
451
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200452 if (alias_normalize(given_alias, alias, NULL) < 0) {
453 DBG(ctx, "invalid alias: %s\n", given_alias);
Lucas De Marchid470db12011-12-13 10:28:00 -0200454 return -EINVAL;
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200455 }
456
457 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200458
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200459 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200460 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200461 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200462
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200463 DBG(ctx, "lookup modules.dep %s\n", alias);
Lucas De Marchi64700e42011-12-01 15:57:53 -0200464 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
465 CHECK_ERR_AND_FINISH(err, fail, list, finish);
466
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200467 DBG(ctx, "lookup modules.symbols %s\n", alias);
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200468 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
469 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200470
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200471 DBG(ctx, "lookup install and remove commands %s\n", alias);
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200472 err = kmod_lookup_alias_from_commands(ctx, alias, list);
473 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200474
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200475 DBG(ctx, "lookup modules.aliases %s\n", alias);
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200476 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
477 CHECK_ERR_AND_FINISH(err, fail, list, finish);
478
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200479finish:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200480 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200481 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200482fail:
Gustavo Sverzut Barbierib55df2e2011-12-20 13:04:10 -0200483 DBG(ctx, "Failed to lookup %s\n", alias);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200484 kmod_module_unref_list(*list);
485 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200486 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200487}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200488#undef CHECK_ERR_AND_FINISH
489
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200490/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200491 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200492 * @list: list of kmod modules
493 *
494 * Drop a reference of each kmod module in @list and releases the resources
495 * taken by the list itself.
496 *
497 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
498 * returns the passed @mod with its refcount decremented.
499 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200500KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
501{
502 for (; list != NULL; list = kmod_list_remove(list))
503 kmod_module_unref(list->data);
504
505 return 0;
506}
507
Lucas De Marchi0d467432011-12-31 11:15:52 -0200508/**
509 * kmod_module_get_filtered_blacklist:
510 * @ctx: kmod library context
511 * @input: list of kmod_module to be filtered with blacklist
512 * @output: where to save the new list
513 *
514 * Given a list @input, this function filter it out with config's blacklist
515 * ans save it in @output.
516 *
517 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
518 * list.
519 */
520KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
521 const struct kmod_list *input,
522 struct kmod_list **output)
523{
524 const struct kmod_list *li;
525 const struct kmod_list *blacklist;
526
527 if (ctx == NULL || output == NULL)
528 return -ENOENT;
529
530 *output = NULL;
531 if (input == NULL)
532 return 0;
533
534 blacklist = kmod_get_blacklists(ctx);
535 kmod_list_foreach(li, input) {
536 struct kmod_module *mod = li->data;
537 const struct kmod_list *lb;
538 struct kmod_list *node;
539 bool filtered = false;
540
541 kmod_list_foreach(lb, blacklist) {
542 const char *name = lb->data;
543
Lucas De Marchi4926cb52011-12-31 11:21:52 -0200544 if (streq(name, mod->name)) {
Lucas De Marchi0d467432011-12-31 11:15:52 -0200545 filtered = true;
546 break;
547 }
548 }
549
550 if (filtered)
551 continue;
552
553 node = kmod_list_append(*output, mod);
554 if (node == NULL)
555 goto fail;
556
557 *output = node;
558 kmod_module_ref(mod);
559 }
560
561 return 0;
562
563fail:
564 kmod_module_unref_list(*output);
565 *output = NULL;
566 return -ENOMEM;
567}
568
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200569static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
570{
571 if (!mod->init.dep) {
572 /* lazy init */
573 char *line = kmod_search_moddep(mod->ctx, mod->name);
574
575 if (line == NULL)
576 return NULL;
577
578 kmod_module_parse_depline((struct kmod_module *)mod, line);
579 free(line);
580
581 if (!mod->init.dep)
582 return NULL;
583 }
584
585 return mod->dep;
586}
587
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200588/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200589 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200590 * @mod: kmod module
591 *
592 * Search the modules.dep index to find the dependencies of the given @mod.
593 * The result is cached in @mod, so subsequent calls to this function will
594 * return the already searched list of modules.
595 *
596 * Returns: NULL on failure or if there are any dependencies. Otherwise it
597 * returns a list of kmod modules that can be released by calling
598 * kmod_module_unref_list().
599 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200600KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200601{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200602 struct kmod_list *l, *l_new, *list_new = NULL;
603
604 if (mod == NULL)
605 return NULL;
606
Lucas De Marchib72f74b2011-12-27 04:51:05 -0200607 module_get_dependencies_noref(mod);
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200608
609 kmod_list_foreach(l, mod->dep) {
610 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
611 if (l_new == NULL) {
612 kmod_module_unref(l->data);
613 goto fail;
614 }
615
616 list_new = l_new;
617 }
618
619 return list_new;
620
621fail:
622 ERR(mod->ctx, "out of memory\n");
623 kmod_module_unref_list(list_new);
624 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200625}
626
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200627/**
628 * kmod_module_get_module:
629 * @entry: an entry in a list of kmod modules.
630 *
631 * Get the kmod module of this @entry in the list, increasing its refcount.
632 * After it's used, unref it. Since the refcount is incremented upon return,
633 * you still have to call kmod_module_unref_list() to release the list of kmod
634 * modules.
635 *
636 * Returns: NULL on failure or the kmod_module contained in this list entry
637 * with its refcount incremented.
638 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200639KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200640{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200641 if (entry == NULL)
642 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200643
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200644 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200645}
646
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200647/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200648 * kmod_module_get_name:
649 * @mod: kmod module
650 *
651 * Get the name of this kmod module. Name is always available, independently
652 * if it was created by kmod_module_new_from_name() or another function and
653 * it's always normalized (dashes are replaced with underscores).
654 *
655 * Returns: the name of this kmod module.
656 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200657KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200658{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200659 if (mod == NULL)
660 return NULL;
661
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200662 return mod->name;
663}
664
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200665/**
666 * kmod_module_get_path:
667 * @mod: kmod module
668 *
669 * Get the path of this kmod module. If this kmod module was not created by
670 * path, it can search the modules.dep index in order to find out the module
Lucas De Marchidb74cee2012-01-09 03:45:19 -0200671 * under context's dirname.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200672 *
673 * Returns: the path of this kmod module or NULL if such information is not
674 * available.
675 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200676KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200677{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200678 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200679
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200680 if (mod == NULL)
681 return NULL;
682
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200683 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200684
Lucas De Marchie005fac2011-12-08 10:42:34 -0200685 if (mod->path != NULL)
686 return mod->path;
687 if (mod->init.dep)
688 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200689
Lucas De Marchie005fac2011-12-08 10:42:34 -0200690 /* lazy init */
691 line = kmod_search_moddep(mod->ctx, mod->name);
692 if (line == NULL)
693 return NULL;
694
695 kmod_module_parse_depline((struct kmod_module *) mod, line);
696 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200697
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200698 return mod->path;
699}
700
701
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200702extern long delete_module(const char *name, unsigned int flags);
703
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200704/**
705 * kmod_module_remove_module:
706 * @mod: kmod module
707 * @flags: flags to pass to Linux kernel when removing the module
708 *
709 * Remove a module from Linux kernel.
710 *
711 * Returns: 0 on success or < 0 on failure.
712 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200713KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
714 unsigned int flags)
715{
716 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200717
718 if (mod == NULL)
719 return -ENOENT;
720
721 /* Filter out other flags */
722 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
723
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200724 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200725 if (err != 0) {
Lucas De Marchiba998b92012-01-11 00:08:14 -0200726 err = -errno;
727 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200728 }
729
Lucas De Marchiba998b92012-01-11 00:08:14 -0200730 return err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200731}
732
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200733extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200734
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200735/**
736 * kmod_module_insert_module:
737 * @mod: kmod module
Lucas De Marchi142db572011-12-20 23:39:30 -0200738 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
739 * behavior of this function.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200740 * @options: module's options to pass to Linux Kernel.
741 *
742 * Insert a module in Linux kernel. It opens the file pointed by @mod,
743 * mmap'ing it and passing to kernel.
744 *
Lucas De Marchibbf59322011-12-30 14:13:33 -0200745 * Returns: 0 on success or < 0 on failure. If module is already loaded it
746 * returns -EEXIST.
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200747 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200748KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200749 unsigned int flags,
750 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200751{
752 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200753 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200754 off_t size;
755 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200756 struct kmod_elf *elf = NULL;
757 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200758 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200759
760 if (mod == NULL)
761 return -ENOENT;
762
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200763 path = kmod_module_get_path(mod);
764 if (path == NULL) {
Dave Reisnerb787b562012-01-04 10:59:49 -0500765 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200766 return -ENOSYS;
767 }
768
Lucas De Marchic68e92f2012-01-04 08:19:34 -0200769 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200770 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200771 err = -errno;
772 return err;
773 }
774
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200775 size = kmod_file_get_size(file);
776 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200777
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200778 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
779 elf = kmod_elf_new(mem, size);
780 if (elf == NULL) {
781 err = -errno;
782 goto elf_failed;
783 }
784
785 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
786 err = kmod_elf_strip_section(elf, "__versions");
787 if (err < 0)
788 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
789 }
790
791 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
792 err = kmod_elf_strip_vermagic(elf);
793 if (err < 0)
794 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
795 }
796
797 mem = kmod_elf_get_memory(elf);
798 }
799
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200800 err = init_module(mem, size, args);
Lucas De Marchibbf59322011-12-30 14:13:33 -0200801 if (err < 0) {
802 err = -errno;
803 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
804 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200805
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200806 if (elf != NULL)
807 kmod_elf_unref(elf);
808elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200809 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200810
811 return err;
812}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200813
Lucas De Marchiddbda022011-12-27 11:40:10 -0200814static bool module_is_blacklisted(struct kmod_module *mod)
815{
816 struct kmod_ctx *ctx = mod->ctx;
817 const struct kmod_list *bl = kmod_get_blacklists(ctx);
818 const struct kmod_list *l;
819
820 kmod_list_foreach(l, bl) {
821 const char *modname = kmod_blacklist_get_modname(l);
822
823 if (streq(modname, mod->name))
824 return true;
825 }
826
827 return false;
828}
829
830#define RECURSION_CHECK_STEP 10
831#define RET_CHECK_NOLOOP_OR_FAIL(_ret, _flags, _label) \
832 do { \
833 if (_ret < 0) { \
834 if (_ret == -ELOOP || _ret == -ENOMEM \
835 || (_flags & KMOD_PROBE_STOP_ON_FAILURE)) \
836 goto _label; \
837 } \
838 } while (0)
839
840struct probe_insert_cb {
841 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
842 void *data;
843};
844
845int module_probe_insert_module(struct kmod_module *mod,
846 unsigned int flags, const char *extra_options,
847 struct probe_insert_cb *cb,
848 struct kmod_list *rec, unsigned int reccount);
849
850static int command_do(struct kmod_module *mod, const char *type,
851 const char *cmd)
852{
853 const char *modname = kmod_module_get_name(mod);
854 int err;
855
856 DBG(mod->ctx, "%s %s\n", type, cmd);
857
858 setenv("MODPROBE_MODULE", modname, 1);
859 err = system(cmd);
860 unsetenv("MODPROBE_MODULE");
861
862 if (err == -1 || WEXITSTATUS(err)) {
863 ERR(mod->ctx, "Error running %s command for %s\n",
864 type, modname);
865 if (err != -1)
866 err = -WEXITSTATUS(err);
867 }
868
869 return err;
870}
871
872static int module_do_install_commands(struct kmod_module *mod,
873 const char *options,
874 struct probe_insert_cb *cb)
875{
876 const char *command = kmod_module_get_install_commands(mod);
877 char *p, *cmd;
878 int err;
879 size_t cmdlen, options_len, varlen;
880
881 assert(command);
882
883 if (options == NULL)
884 options = "";
885
886 options_len = strlen(options);
887 cmdlen = strlen(command);
888 varlen = sizeof("$CMDLINE_OPTS") - 1;
889
890 cmd = memdup(command, cmdlen + 1);
891 if (cmd == NULL)
892 return -ENOMEM;
893
894 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
895 size_t prefixlen = p - cmd;
896 size_t suffixlen = cmdlen - prefixlen - varlen;
897 size_t slen = cmdlen - varlen + options_len;
898 char *suffix = p + varlen;
899 char *s = malloc(slen + 1);
900 if (s == NULL) {
901 free(cmd);
902 return -ENOMEM;
903 }
904 memcpy(s, cmd, p - cmd);
905 memcpy(s + prefixlen, options, options_len);
906 memcpy(s + prefixlen + options_len, suffix, suffixlen);
907 s[slen] = '\0';
908
909 free(cmd);
910 cmd = s;
911 cmdlen = slen;
912 }
913
914 if (cb->run_install != NULL)
915 err = cb->run_install(mod, cmd, cb->data);
916 else
917 err = command_do(mod, "install", cmd);
918
919 free(cmd);
920
921 return err;
922}
923
924static bool module_dep_has_loop(const struct kmod_list *deps,
925 struct kmod_list *rec,
926 unsigned int reccount)
927{
928 struct kmod_list *l;
929 struct kmod_module *mod;
930
931 if (reccount < RECURSION_CHECK_STEP || deps == NULL)
932 return false;
933
934 mod = deps->data;
935 reccount = 0;
936 kmod_list_foreach(l, rec) {
937 struct kmod_list *loop;
938
939 if (l->data != mod)
940 continue;
941
942 ERR(mod->ctx, "Dependency loop detected while inserting '%s'. Operation aborted\n",
943 mod->name);
944
945 for (loop = l; loop != NULL;
946 loop = kmod_list_next(rec, loop)) {
947 struct kmod_module *m = loop->data;
948 ERR(mod->ctx, "%s\n", m->name);
949 }
950
951 return true;
952 }
953
954 return false;
955}
956
957static int module_do_insmod_dep(const struct kmod_list *deps,
958 unsigned int flags, struct probe_insert_cb *cb,
959 struct kmod_list *rec, unsigned int reccount)
960{
961 const struct kmod_list *d;
962 int err = 0;
963
964 if (module_dep_has_loop(deps, rec, reccount))
965 return -ELOOP;
966
967 kmod_list_foreach(d, deps) {
968 struct kmod_module *dm = d->data;
969 struct kmod_list *tmp;
970
971 tmp = kmod_list_append(rec, dm);
972 if (tmp == NULL)
973 return -ENOMEM;
974 rec = tmp;
975
976 err = module_probe_insert_module(dm, flags, NULL, cb,
977 rec, reccount + 1);
978
979 rec = kmod_list_remove_n_latest(rec, 1);
980 RET_CHECK_NOLOOP_OR_FAIL(err, flags, finish);
981 }
982
983finish:
984 return err;
985}
986
987static char *module_options_concat(const char *opt, const char *xopt)
988{
989 // TODO: we might need to check if xopt overrides options on opt
990 size_t optlen = opt == NULL ? 0 : strlen(opt);
991 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
992 char *r;
993
994 if (optlen == 0 && xoptlen == 0)
995 return NULL;
996
997 r = malloc(optlen + xoptlen + 2);
998
999 if (opt != NULL) {
1000 memcpy(r, opt, optlen);
1001 r[optlen] = ' ';
1002 optlen++;
1003 }
1004
1005 if (xopt != NULL)
1006 memcpy(r + optlen, xopt, xoptlen);
1007
1008 r[optlen + xoptlen] = '\0';
1009
1010 return r;
1011}
1012
1013/*
1014 * Do the probe_insert work recursively. We traverse the dependencies in
1015 * depth-first order, checking the following conditions:
1016 *
1017 * - Is blacklisted?
1018 * - Is install command?
1019 * - Is already loaded?
1020 *
1021 * Then we insert the modules (calling module_do_insmod_dep(), which will
1022 * re-enter this function) needed to load @mod in the following order:
1023 *
1024 * 1) pre-softdep
1025 * 2) dependency
1026 * 3) @mod
1027 * 4) post-softdep
1028 */
1029int module_probe_insert_module(struct kmod_module *mod,
1030 unsigned int flags, const char *extra_options,
1031 struct probe_insert_cb *cb,
1032 struct kmod_list *rec, unsigned int reccount)
1033{
1034 int err;
1035 const char *install_cmds;
1036 const struct kmod_list *dep;
1037 struct kmod_list *pre = NULL, *post = NULL;
1038 char *options;
1039
1040 if ((flags & KMOD_PROBE_STOP_ON_BLACKLIST)
1041 && module_is_blacklisted(mod)) {
1042 DBG(mod->ctx, "Stopping on '%s': blacklisted\n", mod->name);
1043 return -EINVAL;
1044 }
1045
1046 install_cmds = kmod_module_get_install_commands(mod);
1047 if (install_cmds != NULL) {
1048 if (flags & KMOD_PROBE_STOP_ON_COMMAND) {
1049 DBG(mod->ctx, "Stopping on '%s': install command\n",
1050 mod->name);
1051 return -EINVAL;
1052 }
1053 } else {
1054 int state = kmod_module_get_initstate(mod);
1055
1056 if (state == KMOD_MODULE_LIVE ||
1057 state == KMOD_MODULE_COMING ||
1058 state == KMOD_MODULE_BUILTIN)
1059 return 0;
1060 }
1061
1062 err = kmod_module_get_softdeps(mod, &pre, &post);
1063 if (err < 0)
1064 return err;
1065
1066 err = module_do_insmod_dep(pre, flags, cb, rec, reccount);
1067 RET_CHECK_NOLOOP_OR_FAIL(err, flags, finish);
1068
1069 dep = module_get_dependencies_noref(mod);
1070 err = module_do_insmod_dep(dep, flags, cb, rec, reccount);
1071 RET_CHECK_NOLOOP_OR_FAIL(err, flags, finish);
1072
1073 options = module_options_concat(kmod_module_get_options(mod),
1074 extra_options);
1075
1076 if (install_cmds != NULL)
1077 err = module_do_install_commands(mod, options, cb);
1078 else
1079 err = kmod_module_insert_module(mod, flags, options);
1080
1081 free(options);
1082
Lucas De Marchie47c6042011-12-30 14:15:40 -02001083 /*
1084 * Ignore "already loaded" error. We need to check here because of
1085 * race conditions. We checked first if module was already loaded but
1086 * it may have been loaded between the check and the moment we try to
1087 * insert it.
1088 */
1089 if (err < 0 && err != -EEXIST && (flags & KMOD_PROBE_STOP_ON_FAILURE))
1090 goto finish;
Lucas De Marchiddbda022011-12-27 11:40:10 -02001091
1092 err = module_do_insmod_dep(post, flags, cb, rec, reccount);
1093
1094finish:
1095 kmod_module_unref_list(pre);
1096 kmod_module_unref_list(post);
1097
1098 return err;
1099}
1100
1101/**
1102 * kmod_module_probe_insert_module:
1103 * @mod: kmod module
1104 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1105 * behavior of this function.
1106 * @extra_options: module's options to pass to Linux Kernel.
1107 * @run_install: function to run when @mod is backed by a install command.
1108 * @data: data to give back to @run_install callback
1109 *
1110 * Insert a module in Linux kernel resolving dependencies, soft dependencies
1111 * install commands and applying blacklist.
1112 *
1113 * If @run_install is NULL, and the flag KMOD_PROBE_STOP_ON_COMMANDS is not
1114 * given, this function will fork and exec by calling system(3). If you need
1115 * control over the execution of an install command, give a callback function
1116 * in @run_install.
1117 *
1118 * Returns: 0 on success or < 0 on failure.
1119 */
1120KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1121 unsigned int flags, const char *extra_options,
1122 int (*run_install)(struct kmod_module *m,
1123 const char *cmd, void *data),
1124 const void *data)
1125{
1126 struct probe_insert_cb cb;
1127
1128 cb.run_install = run_install;
1129 cb.data = (void *) data;
1130
1131 return module_probe_insert_module(mod, flags, extra_options, &cb,
1132 NULL, 0);
1133}
1134
1135#undef RECURSION_CHECK_STEP
1136#undef RET_CHECK_NOLOOP_OR_FAIL
1137
1138
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001139/**
1140 * kmod_module_get_options:
1141 * @mod: kmod module
1142 *
1143 * Get options of this kmod module. Options come from the configuration file
1144 * and are cached in @mod. The first call to this function will search for
1145 * this module in configuration and subsequent calls return the cached string.
1146 *
1147 * Returns: a string with all the options separated by spaces. This string is
1148 * owned by @mod, do not free it.
1149 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001150KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1151{
1152 if (mod == NULL)
1153 return NULL;
1154
1155 if (!mod->init.options) {
1156 /* lazy init */
1157 struct kmod_module *m = (struct kmod_module *)mod;
1158 const struct kmod_list *l, *ctx_options;
1159 char *opts = NULL;
1160 size_t optslen = 0;
1161
1162 ctx_options = kmod_get_options(mod->ctx);
1163
1164 kmod_list_foreach(l, ctx_options) {
1165 const char *modname = kmod_option_get_modname(l);
1166 const char *str;
1167 size_t len;
1168 void *tmp;
1169
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001170 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1171 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1172 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001173 continue;
1174
Lucas De Marchi07b8c822011-12-13 14:21:24 -02001175 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 -02001176 str = kmod_option_get_options(l);
1177 len = strlen(str);
1178 if (len < 1)
1179 continue;
1180
1181 tmp = realloc(opts, optslen + len + 2);
1182 if (tmp == NULL) {
1183 free(opts);
1184 goto failed;
1185 }
1186
1187 opts = tmp;
1188
1189 if (optslen > 0) {
1190 opts[optslen] = ' ';
1191 optslen++;
1192 }
1193
1194 memcpy(opts + optslen, str, len);
1195 optslen += len;
1196 opts[optslen] = '\0';
1197 }
1198
1199 m->init.options = true;
1200 m->options = opts;
1201 }
1202
1203 return mod->options;
1204
1205failed:
1206 ERR(mod->ctx, "out of memory\n");
1207 return NULL;
1208}
1209
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001210/**
1211 * kmod_module_get_install_commands:
1212 * @mod: kmod module
1213 *
1214 * Get install commands for this kmod module. Install commands come from the
1215 * configuration file and are cached in @mod. The first call to this function
1216 * will search for this module in configuration and subsequent calls return
1217 * the cached string. The install commands are returned as they were in the
1218 * configuration, concatenated by ';'. No other processing is made in this
1219 * string.
1220 *
1221 * Returns: a string with all install commands separated by semicolons. This
1222 * string is owned by @mod, do not free it.
1223 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001224KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1225{
1226 if (mod == NULL)
1227 return NULL;
1228
1229 if (!mod->init.install_commands) {
1230 /* lazy init */
1231 struct kmod_module *m = (struct kmod_module *)mod;
1232 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001233
1234 ctx_install_commands = kmod_get_install_commands(mod->ctx);
1235
1236 kmod_list_foreach(l, ctx_install_commands) {
1237 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001238
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001239 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001240 continue;
1241
Lucas De Marchi60f67602011-12-16 03:33:26 -02001242 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001243
Lucas De Marchi60f67602011-12-16 03:33:26 -02001244 /*
1245 * find only the first command, as modprobe from
1246 * module-init-tools does
1247 */
1248 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001249 }
1250
1251 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001252 }
1253
1254 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001255}
1256
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001257void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1258{
1259 mod->init.install_commands = true;
1260 mod->install_commands = cmd;
1261}
1262
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001263static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1264{
1265 struct kmod_list *ret = NULL;
1266 unsigned i;
1267
1268 for (i = 0; i < count; i++) {
1269 const char *depname = array[i];
1270 struct kmod_list *lst = NULL;
1271 int err;
1272
1273 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1274 if (err < 0) {
1275 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1276 continue;
1277 } else if (lst != NULL)
1278 ret = kmod_list_append_list(ret, lst);
1279 }
1280 return ret;
1281}
1282
1283/**
1284 * kmod_module_get_softdeps:
1285 * @mod: kmod module
1286 * @pre: where to save the list of preceding soft dependencies.
1287 * @post: where to save the list of post soft dependencies.
1288 *
1289 * Get soft dependencies for this kmod module. Soft dependencies come
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001290 * from configuration file and are not cached in @mod because it may include
1291 * dependency cycles that would make we leak kmod_module. Any call
1292 * to this function will search for this module in configuration, allocate a
1293 * list and return the result.
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001294 *
1295 * Both @pre and @post are newly created list of kmod_module and
1296 * should be unreferenced with kmod_module_unref_list().
1297 *
1298 * Returns: 0 on success or < 0 otherwise.
1299 */
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001300KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1301 struct kmod_list **pre,
1302 struct kmod_list **post)
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001303{
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001304 const struct kmod_list *l, *ctx_softdeps;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001305
1306 if (mod == NULL || pre == NULL || post == NULL)
1307 return -ENOENT;
1308
1309 assert(*pre == NULL);
1310 assert(*post == NULL);
1311
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001312 ctx_softdeps = kmod_get_softdeps(mod->ctx);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001313
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001314 kmod_list_foreach(l, ctx_softdeps) {
1315 const char *modname = kmod_softdep_get_name(l);
1316 const char * const *array;
1317 unsigned count;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001318
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001319 if (fnmatch(modname, mod->name, 0) != 0)
1320 continue;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001321
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001322 array = kmod_softdep_get_pre(l, &count);
1323 *pre = lookup_softdep(mod->ctx, array, count);
1324 array = kmod_softdep_get_post(l, &count);
1325 *post = lookup_softdep(mod->ctx, array, count);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001326
Lucas De Marchi2bd7cbf2011-12-27 10:15:40 -02001327 /*
1328 * find only the first command, as modprobe from
1329 * module-init-tools does
1330 */
1331 break;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001332 }
1333
1334 return 0;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -02001335}
1336
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001337/**
1338 * kmod_module_get_remove_commands:
1339 * @mod: kmod module
1340 *
1341 * Get remove commands for this kmod module. Remove commands come from the
1342 * configuration file and are cached in @mod. The first call to this function
1343 * will search for this module in configuration and subsequent calls return
1344 * the cached string. The remove commands are returned as they were in the
1345 * configuration, concatenated by ';'. No other processing is made in this
1346 * string.
1347 *
1348 * Returns: a string with all remove commands separated by semicolons. This
1349 * string is owned by @mod, do not free it.
1350 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001351KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1352{
1353 if (mod == NULL)
1354 return NULL;
1355
1356 if (!mod->init.remove_commands) {
1357 /* lazy init */
1358 struct kmod_module *m = (struct kmod_module *)mod;
1359 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001360
1361 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1362
1363 kmod_list_foreach(l, ctx_remove_commands) {
1364 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001365
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001366 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001367 continue;
1368
Lucas De Marchi60f67602011-12-16 03:33:26 -02001369 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001370
Lucas De Marchi60f67602011-12-16 03:33:26 -02001371 /*
1372 * find only the first command, as modprobe from
1373 * module-init-tools does
1374 */
1375 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001376 }
1377
1378 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001379 }
1380
1381 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001382}
1383
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001384void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1385{
1386 mod->init.remove_commands = true;
1387 mod->remove_commands = cmd;
1388}
1389
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001390/**
1391 * SECTION:libkmod-loaded
1392 * @short_description: currently loaded modules
1393 *
1394 * Information about currently loaded modules, as reported by Linux kernel.
1395 * These information are not cached by libkmod and are always read from /sys
1396 * and /proc/modules.
1397 */
1398
1399/**
1400 * kmod_module_new_from_loaded:
1401 * @ctx: kmod library context
1402 * @list: where to save the list of loaded modules
1403 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001404 * Create a new list of kmod modules with all modules currently loaded in
1405 * kernel. It uses /proc/modules to get the names of loaded modules and to
1406 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1407 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001408 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001409 * The initial refcount is 1, and needs to be decremented to release the
1410 * resources of the kmod_module. The returned @list must be released by
1411 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1412 * kmod_modules created, they are all released upon @ctx destruction too. Do
1413 * not unref @ctx before all the desired operations with the returned list are
1414 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001415 *
1416 * Returns: 0 on success or < 0 on error.
1417 */
1418KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1419 struct kmod_list **list)
1420{
1421 struct kmod_list *l = NULL;
1422 FILE *fp;
1423 char line[4096];
1424
1425 if (ctx == NULL || list == NULL)
1426 return -ENOENT;
1427
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001428 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001429 if (fp == NULL) {
1430 int err = -errno;
1431 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1432 return err;
1433 }
1434
1435 while (fgets(line, sizeof(line), fp)) {
1436 struct kmod_module *m;
1437 struct kmod_list *node;
1438 int err;
1439 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1440
1441 err = kmod_module_new_from_name(ctx, name, &m);
1442 if (err < 0) {
1443 ERR(ctx, "could not get module from name '%s': %s\n",
1444 name, strerror(-err));
1445 continue;
1446 }
1447
1448 node = kmod_list_append(l, m);
1449 if (node)
1450 l = node;
1451 else {
1452 ERR(ctx, "out of memory\n");
1453 kmod_module_unref(m);
1454 }
1455 }
1456
1457 fclose(fp);
1458 *list = l;
1459
1460 return 0;
1461}
1462
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001463/**
1464 * kmod_module_initstate_str:
1465 * @state: the state as returned by kmod_module_get_initstate()
1466 *
1467 * Translate a initstate to a string.
1468 *
1469 * Returns: the string associated to the @state. This string is statically
1470 * allocated, do not free it.
1471 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001472KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1473{
1474 switch (state) {
1475 case KMOD_MODULE_BUILTIN:
1476 return "builtin";
1477 case KMOD_MODULE_LIVE:
1478 return "live";
1479 case KMOD_MODULE_COMING:
1480 return "coming";
1481 case KMOD_MODULE_GOING:
1482 return "going";
1483 default:
1484 return NULL;
1485 }
1486}
1487
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001488/**
1489 * kmod_module_get_initstate:
1490 * @mod: kmod module
1491 *
1492 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1493 * /sys filesystem.
1494 *
1495 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1496 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001497KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1498{
1499 char path[PATH_MAX], buf[32];
1500 int fd, err, pathlen;
1501
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001502 if (mod == NULL)
1503 return -ENOENT;
1504
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001505 pathlen = snprintf(path, sizeof(path),
1506 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001507 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001508 if (fd < 0) {
1509 err = -errno;
1510
Lucas De Marchiddbda022011-12-27 11:40:10 -02001511 DBG(mod->ctx, "could not open '%s': %s\n",
1512 path, strerror(-err));
1513
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001514 if (pathlen > (int)sizeof("/initstate") - 1) {
1515 struct stat st;
1516 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1517 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1518 return KMOD_MODULE_BUILTIN;
1519 }
1520
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001521 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001522 path, strerror(-err));
1523 return err;
1524 }
1525
1526 err = read_str_safe(fd, buf, sizeof(buf));
1527 close(fd);
1528 if (err < 0) {
1529 ERR(mod->ctx, "could not read from '%s': %s\n",
1530 path, strerror(-err));
1531 return err;
1532 }
1533
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001534 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001535 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001536 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001537 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001538 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001539 return KMOD_MODULE_GOING;
1540
1541 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1542 return -EINVAL;
1543}
1544
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001545/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001546 * kmod_module_get_size:
1547 * @mod: kmod module
1548 *
1549 * Get the size of this kmod module as returned by Linux kernel. It reads the
1550 * file /proc/modules to search for this module and get its size.
1551 *
1552 * Returns: the size of this kmod module.
1553 */
1554KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1555{
1556 // FIXME TODO: this should be available from /sys/module/foo
1557 FILE *fp;
1558 char line[4096];
1559 int lineno = 0;
1560 long size = -ENOENT;
1561
1562 if (mod == NULL)
1563 return -ENOENT;
1564
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001565 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001566 if (fp == NULL) {
1567 int err = -errno;
1568 ERR(mod->ctx,
1569 "could not open /proc/modules: %s\n", strerror(errno));
1570 return err;
1571 }
1572
1573 while (fgets(line, sizeof(line), fp)) {
1574 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1575 long value;
1576
1577 lineno++;
1578 if (tok == NULL || !streq(tok, mod->name))
1579 continue;
1580
1581 tok = strtok_r(NULL, " \t", &saveptr);
1582 if (tok == NULL) {
1583 ERR(mod->ctx,
1584 "invalid line format at /proc/modules:%d\n", lineno);
1585 break;
1586 }
1587
1588 value = strtol(tok, &endptr, 10);
1589 if (endptr == tok || *endptr != '\0') {
1590 ERR(mod->ctx,
1591 "invalid line format at /proc/modules:%d\n", lineno);
1592 break;
1593 }
1594
1595 size = value;
1596 break;
1597 }
1598 fclose(fp);
1599 return size;
1600}
1601
1602/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001603 * kmod_module_get_refcnt:
1604 * @mod: kmod module
1605 *
1606 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1607 * /sys filesystem.
1608 *
1609 * Returns: 0 on success or < 0 on failure.
1610 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001611KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1612{
1613 char path[PATH_MAX];
1614 long refcnt;
1615 int fd, err;
1616
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001617 if (mod == NULL)
1618 return -ENOENT;
1619
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001620 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001621 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001622 if (fd < 0) {
1623 err = -errno;
1624 ERR(mod->ctx, "could not open '%s': %s\n",
1625 path, strerror(errno));
1626 return err;
1627 }
1628
1629 err = read_str_long(fd, &refcnt, 10);
1630 close(fd);
1631 if (err < 0) {
1632 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1633 path, strerror(-err));
1634 return err;
1635 }
1636
1637 return (int)refcnt;
1638}
1639
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001640/**
1641 * kmod_module_get_holders:
1642 * @mod: kmod module
1643 *
1644 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1645 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1646 *
1647 * Returns: a new list of kmod modules on success or NULL on failure.
1648 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001649KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1650{
1651 char dname[PATH_MAX];
1652 struct kmod_list *list = NULL;
1653 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001654
1655 if (mod == NULL)
1656 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001657
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001658 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1659
1660 d = opendir(dname);
1661 if (d == NULL) {
1662 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001663 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001664 return NULL;
1665 }
1666
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001667 for (;;) {
1668 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001669 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001670 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001671 int err;
1672
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001673 err = readdir_r(d, &de, &entp);
1674 if (err != 0) {
1675 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1676 mod->name, strerror(-err));
1677 goto fail;
1678 }
1679
1680 if (entp == NULL)
1681 break;
1682
1683 if (de.d_name[0] == '.') {
1684 if (de.d_name[1] == '\0' ||
1685 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001686 continue;
1687 }
1688
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001689 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001690 if (err < 0) {
1691 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001692 de.d_name, strerror(-err));
1693 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001694 }
1695
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001696 l = kmod_list_append(list, holder);
1697 if (l != NULL) {
1698 list = l;
1699 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001700 ERR(mod->ctx, "out of memory\n");
1701 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001702 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001703 }
1704 }
1705
1706 closedir(d);
1707 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001708
1709fail:
1710 closedir(d);
1711 kmod_module_unref_list(list);
1712 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001713}
1714
1715struct kmod_module_section {
1716 unsigned long address;
1717 char name[];
1718};
1719
1720static void kmod_module_section_free(struct kmod_module_section *section)
1721{
1722 free(section);
1723}
1724
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001725/**
1726 * kmod_module_get_sections:
1727 * @mod: kmod module
1728 *
1729 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1730 * structure contained in this list is internal to libkmod and their fields
1731 * can be obtained by calling kmod_module_section_get_name() and
1732 * kmod_module_section_get_address().
1733 *
1734 * After use, free the @list by calling kmod_module_section_free_list().
1735 *
1736 * Returns: a new list of kmod module sections on success or NULL on failure.
1737 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001738KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1739{
1740 char dname[PATH_MAX];
1741 struct kmod_list *list = NULL;
1742 DIR *d;
1743 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001744
1745 if (mod == NULL)
1746 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001747
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001748 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1749
1750 d = opendir(dname);
1751 if (d == NULL) {
1752 ERR(mod->ctx, "could not open '%s': %s\n",
1753 dname, strerror(errno));
1754 return NULL;
1755 }
1756
1757 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001758
1759 for (;;) {
1760 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001761 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001762 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001763 unsigned long address;
1764 size_t namesz;
1765 int fd, err;
1766
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001767 err = readdir_r(d, &de, &entp);
1768 if (err != 0) {
1769 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1770 mod->name, strerror(-err));
1771 goto fail;
1772 }
1773
1774 if (de.d_name[0] == '.') {
1775 if (de.d_name[1] == '\0' ||
1776 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001777 continue;
1778 }
1779
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001780 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001781 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001782 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1783 dname, de.d_name);
1784 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001785 }
1786
1787 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001788 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001789 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001790 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1791 dname, de.d_name);
1792 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001793 }
1794
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001795 namesz = strlen(de.d_name) + 1;
1796 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001797
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001798 if (section == NULL) {
1799 ERR(mod->ctx, "out of memory\n");
1800 goto fail;
1801 }
1802
1803 section->address = address;
1804 memcpy(section->name, de.d_name, namesz);
1805
1806 l = kmod_list_append(list, section);
1807 if (l != NULL) {
1808 list = l;
1809 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001810 ERR(mod->ctx, "out of memory\n");
1811 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001812 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001813 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001814 }
1815
1816 closedir(d);
1817 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001818
1819fail:
1820 closedir(d);
1821 kmod_module_unref_list(list);
1822 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001823}
1824
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001825/**
1826 * kmod_module_section_get_module_name:
1827 * @entry: a list entry representing a kmod module section
1828 *
1829 * Get the name of a kmod module section.
1830 *
1831 * After use, free the @list by calling kmod_module_section_free_list().
1832 *
1833 * Returns: the name of this kmod module section on success or NULL on
1834 * failure. The string is owned by the section, do not free it.
1835 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001836KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1837{
1838 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001839
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001840 if (entry == NULL)
1841 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001842
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001843 section = entry->data;
1844 return section->name;
1845}
1846
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001847/**
1848 * kmod_module_section_get_address:
1849 * @entry: a list entry representing a kmod module section
1850 *
1851 * Get the address of a kmod module section.
1852 *
1853 * After use, free the @list by calling kmod_module_section_free_list().
1854 *
1855 * Returns: the address of this kmod module section on success or ULONG_MAX
1856 * on failure.
1857 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001858KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1859{
1860 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001861
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001862 if (entry == NULL)
1863 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001864
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001865 section = entry->data;
1866 return section->address;
1867}
1868
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001869/**
1870 * kmod_module_section_free_list:
1871 * @list: kmod module section list
1872 *
1873 * Release the resources taken by @list
1874 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001875KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1876{
1877 while (list) {
1878 kmod_module_section_free(list->data);
1879 list = kmod_list_remove(list);
1880 }
1881}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001882
1883struct kmod_module_info {
1884 char *key;
1885 char value[];
1886};
1887
1888static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
1889{
1890 struct kmod_module_info *info;
1891
1892 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
1893 if (info == NULL)
1894 return NULL;
1895
1896 info->key = (char *)info + sizeof(struct kmod_module_info)
1897 + valuelen + 1;
1898 memcpy(info->key, key, keylen);
1899 info->key[keylen] = '\0';
1900 memcpy(info->value, value, valuelen);
1901 info->value[valuelen] = '\0';
1902 return info;
1903}
1904
1905static void kmod_module_info_free(struct kmod_module_info *info)
1906{
1907 free(info);
1908}
1909
1910/**
1911 * kmod_module_get_info:
1912 * @mod: kmod module
1913 * @list: where to return list of module information. Use
1914 * kmod_module_info_get_key() and
1915 * kmod_module_info_get_value(). Release this list with
Lucas De Marchidb74cee2012-01-09 03:45:19 -02001916 * kmod_module_info_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001917 *
1918 * Get a list of entries in ELF section ".modinfo", these contain
1919 * alias, license, depends, vermagic and other keys with respective
1920 * values.
1921 *
1922 * After use, free the @list by calling kmod_module_info_free_list().
1923 *
1924 * Returns: 0 on success or < 0 otherwise.
1925 */
1926KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
1927{
1928 struct kmod_file *file;
1929 struct kmod_elf *elf;
1930 const char *path;
1931 const void *mem;
1932 char **strings;
1933 size_t size;
1934 int i, count, ret = 0;
1935
1936 if (mod == NULL || list == NULL)
1937 return -ENOENT;
1938
1939 assert(*list == NULL);
1940
1941 path = kmod_module_get_path(mod);
1942 if (path == NULL)
1943 return -ENOENT;
1944
Lucas De Marchic68e92f2012-01-04 08:19:34 -02001945 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001946 if (file == NULL)
1947 return -errno;
1948
1949 size = kmod_file_get_size(file);
1950 mem = kmod_file_get_contents(file);
1951
1952 elf = kmod_elf_new(mem, size);
1953 if (elf == NULL) {
1954 ret = -errno;
1955 goto elf_open_error;
1956 }
1957
1958 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
1959 if (count < 0) {
1960 ret = count;
1961 goto get_strings_error;
1962 }
1963
1964 for (i = 0; i < count; i++) {
1965 struct kmod_module_info *info;
1966 struct kmod_list *n;
1967 const char *key, *value;
1968 size_t keylen, valuelen;
1969
1970 key = strings[i];
1971 value = strchr(key, '=');
1972 if (value == NULL) {
1973 keylen = strlen(key);
1974 valuelen = 0;
1975 } else {
1976 keylen = value - key;
1977 value++;
1978 valuelen = strlen(value);
1979 }
1980
1981 info = kmod_module_info_new(key, keylen, value, valuelen);
1982 if (info == NULL) {
1983 ret = -errno;
1984 kmod_module_info_free_list(*list);
1985 *list = NULL;
1986 goto list_error;
1987 }
1988
1989 n = kmod_list_append(*list, info);
1990 if (n != NULL)
1991 *list = n;
1992 else {
1993 kmod_module_info_free(info);
1994 kmod_module_info_free_list(*list);
1995 *list = NULL;
1996 ret = -ENOMEM;
1997 goto list_error;
1998 }
1999 }
2000 ret = count;
2001
2002list_error:
2003 free(strings);
2004get_strings_error:
2005 kmod_elf_unref(elf);
2006elf_open_error:
2007 kmod_file_unref(file);
2008
2009 return ret;
2010}
2011
2012/**
2013 * kmod_module_info_get_key:
2014 * @entry: a list entry representing a kmod module info
2015 *
2016 * Get the key of a kmod module info.
2017 *
2018 * Returns: the key of this kmod module info on success or NULL on
2019 * failure. The string is owned by the info, do not free it.
2020 */
2021KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2022{
2023 struct kmod_module_info *info;
2024
2025 if (entry == NULL)
2026 return NULL;
2027
2028 info = entry->data;
2029 return info->key;
2030}
2031
2032/**
2033 * kmod_module_info_get_value:
2034 * @entry: a list entry representing a kmod module info
2035 *
2036 * Get the value of a kmod module info.
2037 *
2038 * Returns: the value of this kmod module info on success or NULL on
2039 * failure. The string is owned by the info, do not free it.
2040 */
2041KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2042{
2043 struct kmod_module_info *info;
2044
2045 if (entry == NULL)
2046 return NULL;
2047
2048 info = entry->data;
2049 return info->value;
2050}
2051
2052/**
2053 * kmod_module_info_free_list:
2054 * @list: kmod module info list
2055 *
2056 * Release the resources taken by @list
2057 */
2058KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2059{
2060 while (list) {
2061 kmod_module_info_free(list->data);
2062 list = kmod_list_remove(list);
2063 }
2064}
2065
2066struct kmod_module_version {
2067 uint64_t crc;
2068 char symbol[];
2069};
2070
2071static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2072{
2073 struct kmod_module_version *mv;
2074 size_t symbollen = strlen(symbol) + 1;
2075
2076 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2077 if (mv == NULL)
2078 return NULL;
2079
2080 mv->crc = crc;
2081 memcpy(mv->symbol, symbol, symbollen);
2082 return mv;
2083}
2084
2085static void kmod_module_version_free(struct kmod_module_version *version)
2086{
2087 free(version);
2088}
2089
2090/**
2091 * kmod_module_get_versions:
2092 * @mod: kmod module
2093 * @list: where to return list of module versions. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002094 * kmod_module_version_get_symbol() and
2095 * kmod_module_version_get_crc(). Release this list with
2096 * kmod_module_versions_free_list()
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002097 *
2098 * Get a list of entries in ELF section "__versions".
2099 *
2100 * After use, free the @list by calling kmod_module_versions_free_list().
2101 *
2102 * Returns: 0 on success or < 0 otherwise.
2103 */
2104KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2105{
2106 struct kmod_file *file;
2107 struct kmod_elf *elf;
2108 const char *path;
2109 const void *mem;
2110 struct kmod_modversion *versions;
2111 size_t size;
2112 int i, count, ret = 0;
2113
2114 if (mod == NULL || list == NULL)
2115 return -ENOENT;
2116
2117 assert(*list == NULL);
2118
2119 path = kmod_module_get_path(mod);
2120 if (path == NULL)
2121 return -ENOENT;
2122
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002123 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02002124 if (file == NULL)
2125 return -errno;
2126
2127 size = kmod_file_get_size(file);
2128 mem = kmod_file_get_contents(file);
2129
2130 elf = kmod_elf_new(mem, size);
2131 if (elf == NULL) {
2132 ret = -errno;
2133 goto elf_open_error;
2134 }
2135
2136 count = kmod_elf_get_modversions(elf, &versions);
2137 if (count < 0) {
2138 ret = count;
2139 goto get_strings_error;
2140 }
2141
2142 for (i = 0; i < count; i++) {
2143 struct kmod_module_version *mv;
2144 struct kmod_list *n;
2145
2146 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2147 if (mv == NULL) {
2148 ret = -errno;
2149 kmod_module_versions_free_list(*list);
2150 *list = NULL;
2151 goto list_error;
2152 }
2153
2154 n = kmod_list_append(*list, mv);
2155 if (n != NULL)
2156 *list = n;
2157 else {
2158 kmod_module_version_free(mv);
2159 kmod_module_versions_free_list(*list);
2160 *list = NULL;
2161 ret = -ENOMEM;
2162 goto list_error;
2163 }
2164 }
2165 ret = count;
2166
2167list_error:
2168 free(versions);
2169get_strings_error:
2170 kmod_elf_unref(elf);
2171elf_open_error:
2172 kmod_file_unref(file);
2173
2174 return ret;
2175}
2176
2177/**
2178 * kmod_module_versions_get_symbol:
2179 * @entry: a list entry representing a kmod module versions
2180 *
2181 * Get the symbol of a kmod module versions.
2182 *
2183 * Returns: the symbol of this kmod module versions on success or NULL
2184 * on failure. The string is owned by the versions, do not free it.
2185 */
2186KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2187{
2188 struct kmod_module_version *version;
2189
2190 if (entry == NULL)
2191 return NULL;
2192
2193 version = entry->data;
2194 return version->symbol;
2195}
2196
2197/**
2198 * kmod_module_version_get_crc:
2199 * @entry: a list entry representing a kmod module version
2200 *
2201 * Get the crc of a kmod module version.
2202 *
2203 * Returns: the crc of this kmod module version on success or NULL on
2204 * failure. The string is owned by the version, do not free it.
2205 */
2206KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2207{
2208 struct kmod_module_version *version;
2209
2210 if (entry == NULL)
2211 return 0;
2212
2213 version = entry->data;
2214 return version->crc;
2215}
2216
2217/**
2218 * kmod_module_versions_free_list:
2219 * @list: kmod module versions list
2220 *
2221 * Release the resources taken by @list
2222 */
2223KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2224{
2225 while (list) {
2226 kmod_module_version_free(list->data);
2227 list = kmod_list_remove(list);
2228 }
2229}
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002230
2231struct kmod_module_symbol {
2232 uint64_t crc;
2233 char symbol[];
2234};
2235
2236static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2237{
2238 struct kmod_module_symbol *mv;
2239 size_t symbollen = strlen(symbol) + 1;
2240
2241 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2242 if (mv == NULL)
2243 return NULL;
2244
2245 mv->crc = crc;
2246 memcpy(mv->symbol, symbol, symbollen);
2247 return mv;
2248}
2249
2250static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2251{
2252 free(symbol);
2253}
2254
2255/**
2256 * kmod_module_get_symbols:
2257 * @mod: kmod module
2258 * @list: where to return list of module symbols. Use
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002259 * kmod_module_symbol_get_symbol() and
2260 * kmod_module_symbol_get_crc(). Release this list with
2261 * kmod_module_symbols_free_list()
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002262 *
2263 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2264 *
2265 * After use, free the @list by calling kmod_module_symbols_free_list().
2266 *
2267 * Returns: 0 on success or < 0 otherwise.
2268 */
2269KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2270{
2271 struct kmod_file *file;
2272 struct kmod_elf *elf;
2273 const char *path;
2274 const void *mem;
2275 struct kmod_modversion *symbols;
2276 size_t size;
2277 int i, count, ret = 0;
2278
2279 if (mod == NULL || list == NULL)
2280 return -ENOENT;
2281
2282 assert(*list == NULL);
2283
2284 path = kmod_module_get_path(mod);
2285 if (path == NULL)
2286 return -ENOENT;
2287
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002288 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002289 if (file == NULL)
2290 return -errno;
2291
2292 size = kmod_file_get_size(file);
2293 mem = kmod_file_get_contents(file);
2294
2295 elf = kmod_elf_new(mem, size);
2296 if (elf == NULL) {
2297 ret = -errno;
2298 goto elf_open_error;
2299 }
2300
2301 count = kmod_elf_get_symbols(elf, &symbols);
2302 if (count < 0) {
2303 ret = count;
2304 goto get_strings_error;
2305 }
2306
2307 for (i = 0; i < count; i++) {
2308 struct kmod_module_symbol *mv;
2309 struct kmod_list *n;
2310
2311 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2312 if (mv == NULL) {
2313 ret = -errno;
2314 kmod_module_symbols_free_list(*list);
2315 *list = NULL;
2316 goto list_error;
2317 }
2318
2319 n = kmod_list_append(*list, mv);
2320 if (n != NULL)
2321 *list = n;
2322 else {
2323 kmod_module_symbol_free(mv);
2324 kmod_module_symbols_free_list(*list);
2325 *list = NULL;
2326 ret = -ENOMEM;
2327 goto list_error;
2328 }
2329 }
2330 ret = count;
2331
2332list_error:
2333 free(symbols);
2334get_strings_error:
2335 kmod_elf_unref(elf);
2336elf_open_error:
2337 kmod_file_unref(file);
2338
2339 return ret;
2340}
2341
2342/**
Lucas De Marchidb74cee2012-01-09 03:45:19 -02002343 * kmod_module_symbol_get_symbol:
Gustavo Sverzut Barbieri45e6db92011-12-19 21:23:13 -02002344 * @entry: a list entry representing a kmod module symbols
2345 *
2346 * Get the symbol of a kmod module symbols.
2347 *
2348 * Returns: the symbol of this kmod module symbols on success or NULL
2349 * on failure. The string is owned by the symbols, do not free it.
2350 */
2351KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2352{
2353 struct kmod_module_symbol *symbol;
2354
2355 if (entry == NULL)
2356 return NULL;
2357
2358 symbol = entry->data;
2359 return symbol->symbol;
2360}
2361
2362/**
2363 * kmod_module_symbol_get_crc:
2364 * @entry: a list entry representing a kmod module symbol
2365 *
2366 * Get the crc of a kmod module symbol.
2367 *
2368 * Returns: the crc of this kmod module symbol on success or NULL on
2369 * failure. The string is owned by the symbol, do not free it.
2370 */
2371KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2372{
2373 struct kmod_module_symbol *symbol;
2374
2375 if (entry == NULL)
2376 return 0;
2377
2378 symbol = entry->data;
2379 return symbol->crc;
2380}
2381
2382/**
2383 * kmod_module_symbols_free_list:
2384 * @list: kmod module symbols list
2385 *
2386 * Release the resources taken by @list
2387 */
2388KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2389{
2390 while (list) {
2391 kmod_module_symbol_free(list->data);
2392 list = kmod_list_remove(list);
2393 }
2394}
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002395
2396struct kmod_module_dependency_symbol {
2397 uint64_t crc;
2398 uint8_t bind;
2399 char symbol[];
2400};
2401
2402static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2403{
2404 struct kmod_module_dependency_symbol *mv;
2405 size_t symbollen = strlen(symbol) + 1;
2406
2407 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2408 if (mv == NULL)
2409 return NULL;
2410
2411 mv->crc = crc;
2412 mv->bind = bind;
2413 memcpy(mv->symbol, symbol, symbollen);
2414 return mv;
2415}
2416
2417static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2418{
2419 free(dependency_symbol);
2420}
2421
2422/**
2423 * kmod_module_get_dependency_symbols:
2424 * @mod: kmod module
2425 * @list: where to return list of module dependency_symbols. Use
2426 * kmod_module_dependency_symbol_get_symbol() and
2427 * kmod_module_dependency_symbol_get_crc(). Release this list with
2428 * kmod_module_dependency_symbols_free_list()
2429 *
2430 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2431 *
2432 * After use, free the @list by calling
2433 * kmod_module_dependency_symbols_free_list().
2434 *
2435 * Returns: 0 on success or < 0 otherwise.
2436 */
2437KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2438{
2439 struct kmod_file *file;
2440 struct kmod_elf *elf;
2441 const char *path;
2442 const void *mem;
2443 struct kmod_modversion *symbols;
2444 size_t size;
2445 int i, count, ret = 0;
2446
2447 if (mod == NULL || list == NULL)
2448 return -ENOENT;
2449
2450 assert(*list == NULL);
2451
2452 path = kmod_module_get_path(mod);
2453 if (path == NULL)
2454 return -ENOENT;
2455
Lucas De Marchic68e92f2012-01-04 08:19:34 -02002456 file = kmod_file_open(mod->ctx, path);
Gustavo Sverzut Barbieri674f8592011-12-20 11:54:53 -02002457 if (file == NULL)
2458 return -errno;
2459
2460 size = kmod_file_get_size(file);
2461 mem = kmod_file_get_contents(file);
2462
2463 elf = kmod_elf_new(mem, size);
2464 if (elf == NULL) {
2465 ret = -errno;
2466 goto elf_open_error;
2467 }
2468
2469 count = kmod_elf_get_dependency_symbols(elf, &symbols);
2470 if (count < 0) {
2471 ret = count;
2472 goto get_strings_error;
2473 }
2474
2475 for (i = 0; i < count; i++) {
2476 struct kmod_module_dependency_symbol *mv;
2477 struct kmod_list *n;
2478
2479 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2480 symbols[i].bind,
2481 symbols[i].symbol);
2482 if (mv == NULL) {
2483 ret = -errno;
2484 kmod_module_dependency_symbols_free_list(*list);
2485 *list = NULL;
2486 goto list_error;
2487 }
2488
2489 n = kmod_list_append(*list, mv);
2490 if (n != NULL)
2491 *list = n;
2492 else {
2493 kmod_module_dependency_symbol_free(mv);
2494 kmod_module_dependency_symbols_free_list(*list);
2495 *list = NULL;
2496 ret = -ENOMEM;
2497 goto list_error;
2498 }
2499 }
2500 ret = count;
2501
2502list_error:
2503 free(symbols);
2504get_strings_error:
2505 kmod_elf_unref(elf);
2506elf_open_error:
2507 kmod_file_unref(file);
2508
2509 return ret;
2510}
2511
2512/**
2513 * kmod_module_dependency_symbol_get_symbol:
2514 * @entry: a list entry representing a kmod module dependency_symbols
2515 *
2516 * Get the dependency symbol of a kmod module
2517 *
2518 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2519 * on failure. The string is owned by the dependency_symbols, do not free it.
2520 */
2521KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2522{
2523 struct kmod_module_dependency_symbol *dependency_symbol;
2524
2525 if (entry == NULL)
2526 return NULL;
2527
2528 dependency_symbol = entry->data;
2529 return dependency_symbol->symbol;
2530}
2531
2532/**
2533 * kmod_module_dependency_symbol_get_crc:
2534 * @entry: a list entry representing a kmod module dependency_symbol
2535 *
2536 * Get the crc of a kmod module dependency_symbol.
2537 *
2538 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2539 * failure. The string is owned by the dependency_symbol, do not free it.
2540 */
2541KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2542{
2543 struct kmod_module_dependency_symbol *dependency_symbol;
2544
2545 if (entry == NULL)
2546 return 0;
2547
2548 dependency_symbol = entry->data;
2549 return dependency_symbol->crc;
2550}
2551
2552/**
2553 * kmod_module_dependency_symbol_get_bind:
2554 * @entry: a list entry representing a kmod module dependency_symbol
2555 *
2556 * Get the bind type of a kmod module dependency_symbol.
2557 *
2558 * Returns: the bind of this kmod module dependency_symbol on success
2559 * or < 0 on failure.
2560 */
2561KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2562{
2563 struct kmod_module_dependency_symbol *dependency_symbol;
2564
2565 if (entry == NULL)
2566 return 0;
2567
2568 dependency_symbol = entry->data;
2569 return dependency_symbol->bind;
2570}
2571
2572/**
2573 * kmod_module_dependency_symbols_free_list:
2574 * @list: kmod module dependency_symbols list
2575 *
2576 * Release the resources taken by @list
2577 */
2578KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2579{
2580 while (list) {
2581 kmod_module_dependency_symbol_free(list->data);
2582 list = kmod_list_remove(list);
2583 }
2584}