blob: a586201383d607db60c4611528f275d5443fe49e [file] [log] [blame]
Lucas De Marchi8f788d52011-11-25 01:22:56 -02001/*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011 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/**
43 * kmod_module:
44 *
45 * Opaque object representing a module.
46 */
47struct kmod_module {
48 struct kmod_ctx *ctx;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -020049 char *hashkey;
Lucas De Marchi219f9c32011-12-13 13:07:40 -020050 char *name;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020051 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020052 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020053 char *options;
Lucas De Marchi60f67602011-12-16 03:33:26 -020054 const char *install_commands; /* owned by kmod_config */
55 const char *remove_commands; /* owned by kmod_config */
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020056 struct {
57 struct kmod_list *pre;
58 struct kmod_list *post;
59 } softdeps;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -020060 char *alias; /* only set if this module was created from an alias */
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020061 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020062 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020063 struct {
64 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020065 bool options : 1;
66 bool install_commands : 1;
67 bool remove_commands : 1;
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -020068 bool softdeps : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020069 } init;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020070};
71
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -020072inline char *modname_normalize(const char *modname, char buf[NAME_MAX],
Lucas De Marchi6c343b12011-12-06 09:01:01 -020073 size_t *len)
Lucas De Marchi8f788d52011-11-25 01:22:56 -020074{
Lucas De Marchid753b8c2011-12-05 18:14:51 -020075 size_t s;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020076
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020077 for (s = 0; s < NAME_MAX - 1; s++) {
78 const char c = modname[s];
79 if (c == '-')
80 buf[s] = '_';
81 else if (c == '\0' || c == '.')
82 break;
83 else
84 buf[s] = c;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020085 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -020086
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020087 buf[s] = '\0';
Lucas De Marchid753b8c2011-12-05 18:14:51 -020088
89 if (len)
90 *len = s;
91
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020092 return buf;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020093}
94
Lucas De Marchi6c343b12011-12-06 09:01:01 -020095static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len)
96{
97 char *modname;
98
99 modname = basename(path);
100 if (modname == NULL || modname[0] == '\0')
101 return NULL;
102
103 return modname_normalize(modname, buf, len);
104}
105
Lucas De Marchic35347f2011-12-12 10:48:02 -0200106static inline const char *path_join(const char *path, size_t prefixlen,
107 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200108{
109 size_t pathlen;
110
111 if (path[0] == '/')
112 return path;
113
114 pathlen = strlen(path);
115 if (prefixlen + pathlen + 1 >= PATH_MAX)
116 return NULL;
117
118 memcpy(buf + prefixlen, path, pathlen + 1);
119 return buf;
120}
121
Lucas De Marchi671d4892011-12-05 20:23:05 -0200122int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200123{
124 struct kmod_ctx *ctx = mod->ctx;
125 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200126 const char *dirname;
127 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200128 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200129 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200130 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200131
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200132 if (mod->init.dep)
133 return mod->n_dep;
134 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200135 mod->init.dep = true;
136
137 p = strchr(line, ':');
138 if (p == NULL)
139 return 0;
140
Lucas De Marchi671d4892011-12-05 20:23:05 -0200141 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200142 dirname = kmod_get_dirname(mod->ctx);
143 dirnamelen = strlen(dirname);
144 if (dirnamelen + 2 >= PATH_MAX)
145 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200146
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200147 memcpy(buf, dirname, dirnamelen);
148 buf[dirnamelen] = '/';
149 dirnamelen++;
150 buf[dirnamelen] = '\0';
151
152 if (mod->path == NULL) {
153 const char *str = path_join(line, dirnamelen, buf);
154 if (str == NULL)
155 return 0;
156 mod->path = strdup(str);
157 if (mod->path == NULL)
158 return 0;
159 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200160
Lucas De Marchi7636e722011-12-01 17:56:03 -0200161 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200162 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
163 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200164 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200165 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200166
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200167 path = path_join(p, dirnamelen, buf);
168 if (path == NULL) {
169 ERR(ctx, "could not join path '%s' and '%s'.\n",
170 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200171 goto fail;
172 }
173
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200174 err = kmod_module_new_from_path(ctx, path, &depmod);
175 if (err < 0) {
176 ERR(ctx, "ctx=%p path=%s error=%s\n",
177 ctx, path, strerror(-err));
178 goto fail;
179 }
180
181 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200182
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200183 list = kmod_list_append(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200184 n++;
185 }
186
187 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
188
189 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200190 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200191 return n;
192
193fail:
194 kmod_module_unref_list(list);
195 mod->init.dep = false;
196 return err;
197}
198
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200199/**
200 * kmod_module_new_from_name:
201 * @ctx: kmod library context
202 * @name: name of the module
203 * @mod: where to save the created struct kmod_module
204 *
205 * Create a new struct kmod_module using the module name. @name can not be an
206 * alias, file name or anything else; it must be a module name. There's no
207 * check if the module does exists in the system.
208 *
209 * This function is also used internally by many others that return a new
210 * struct kmod_module or a new list of modules.
211 *
212 * The initial refcount is 1, and needs to be decremented to release the
213 * resources of the kmod_module. Since libkmod keeps track of all
214 * kmod_modules created, they are all released upon @ctx destruction too. Do
215 * not unref @ctx before all the desired operations with the returned
216 * kmod_module are done.
217 *
218 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
219 * module name or if memory allocation failed.
220 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200221KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
222 const char *name,
223 struct kmod_module **mod)
224{
225 struct kmod_module *m;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200226 size_t namelen;
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200227 char name_norm[NAME_MAX];
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200228 char *namesep;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200229
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200230 if (ctx == NULL || name == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200231 return -ENOENT;
232
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200233 alias_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200234
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200235 m = kmod_pool_get_module(ctx, name_norm);
236 if (m != NULL) {
237 *mod = kmod_module_ref(m);
238 return 0;
239 }
240
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200241 namesep = strchr(name_norm, '/');
242 m = malloc(sizeof(*m) + (namesep == NULL ? 1 : 2) * namelen + 2);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200243 if (m == NULL) {
244 free(m);
245 return -ENOMEM;
246 }
247
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200248 memset(m, 0, sizeof(*m));
249
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200250 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200251 m->name = (char *)m + sizeof(*m);
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200252 memcpy(m->name, name_norm, namelen + 1);
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200253
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200254 if (namesep) {
255 size_t len = namesep - name_norm;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200256
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200257 m->name[len] = '\0';
258 m->alias = m->name + len + 1;
259 m->hashkey = m->name + namelen + 1;
260 memcpy(m->hashkey, name_norm, namelen + 1);
261 } else {
262 m->hashkey = m->name;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200263 }
264
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200265 m->refcount = 1;
266 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200267 *mod = m;
268
269 return 0;
270}
271
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200272int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
273 const char *name, struct kmod_module **mod)
274{
275 int err;
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200276 char key[NAME_MAX];
277 size_t namelen = strlen(name);
278 size_t aliaslen = strlen(alias);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200279
Lucas De Marchi113c66a2011-12-14 15:21:10 -0200280 if (namelen + aliaslen + 2 > NAME_MAX)
281 return -ENAMETOOLONG;
282
283 memcpy(key, name, namelen);
284 memcpy(key + namelen + 1, alias, aliaslen + 1);
285 key[namelen] = '/';
286
287 err = kmod_module_new_from_name(ctx, key, mod);
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200288 if (err < 0)
289 return err;
290
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200291 return 0;
Lucas De Marchi6ad5f262011-12-13 14:12:50 -0200292}
293
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200294/**
295 * kmod_module_new_from_path:
296 * @ctx: kmod library context
297 * @path: path where to find the given module
298 * @mod: where to save the created struct kmod_module
299 *
300 * Create a new struct kmod_module using the module path. @path must be an
301 * existent file with in the filesystem and must be accessible to libkmod.
302 *
303 * The initial refcount is 1, and needs to be decremented to release the
304 * resources of the kmod_module. Since libkmod keeps track of all
305 * kmod_modules created, they are all released upon @ctx destruction too. Do
306 * not unref @ctx before all the desired operations with the returned
307 * kmod_module are done.
308 *
309 * If @path is relative, it's treated as relative to the current working
310 * directory. Otherwise, give an absolute path.
311 *
312 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
313 * it's not a valid file for a kmod_module or if memory allocation failed.
314 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200315KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
316 const char *path,
317 struct kmod_module **mod)
318{
319 struct kmod_module *m;
320 int err;
321 struct stat st;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200322 char name[NAME_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200323 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200324 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200325
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200326 if (ctx == NULL || path == NULL || mod == NULL)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200327 return -ENOENT;
328
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200329 abspath = path_make_absolute_cwd(path);
330 if (abspath == NULL)
331 return -ENOMEM;
332
333 err = stat(abspath, &st);
334 if (err < 0) {
335 free(abspath);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200336 return -errno;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200337 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200338
Gustavo Sverzut Barbieri973c80b2011-12-12 18:28:52 -0200339 if (path_to_modname(path, name, &namelen) == NULL) {
340 free(abspath);
341 return -ENOENT;
342 }
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200343
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200344 m = kmod_pool_get_module(ctx, name);
345 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200346 if (m->path == NULL)
347 m->path = abspath;
348 else if (streq(m->path, abspath))
349 free(abspath);
350 else {
351 ERR(ctx, "kmod_module '%s' already exists with different path\n",
352 name);
353 free(abspath);
354 return -EEXIST;
355 }
356
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200357 *mod = kmod_module_ref(m);
358 return 0;
359 }
360
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200361 m = malloc(sizeof(*m) + namelen + 1);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200362 if (m == NULL)
363 return -errno;
364
Lucas De Marchi788ef0f2011-12-14 15:05:03 -0200365 memset(m, 0, sizeof(*m));
366
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200367 m->ctx = kmod_ref(ctx);
Lucas De Marchi219f9c32011-12-13 13:07:40 -0200368 m->name = (char *)m + sizeof(*m);
Lucas De Marchi9dec2442011-12-18 15:12:19 -0200369 memcpy(m->name, name, namelen + 1);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200370 m->path = abspath;
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200371 m->hashkey = m->name;
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200372 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200373
Lucas De Marchi8bdeca12011-12-15 13:11:51 -0200374 kmod_pool_add_module(ctx, m, m->hashkey);
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200375
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200376 *mod = m;
377
378 return 0;
379}
380
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200381/**
382 * kmod_module_unref:
383 * @mod: kmod module
384 *
385 * Drop a reference of the kmod module. If the refcount reaches zero, its
386 * resources are released.
387 *
388 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
389 * returns the passed @mod with its refcount decremented.
390 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200391KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
392{
393 if (mod == NULL)
394 return NULL;
395
396 if (--mod->refcount > 0)
397 return mod;
398
399 DBG(mod->ctx, "kmod_module %p released\n", mod);
400
Lucas De Marchi4084c172011-12-15 13:43:22 -0200401 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200402 kmod_module_unref_list(mod->softdeps.pre);
403 kmod_module_unref_list(mod->softdeps.post);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200404 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200405 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200406 free(mod->options);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200407 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200408 free(mod);
409 return NULL;
410}
411
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200412/**
413 * kmod_module_ref:
414 * @mod: kmod module
415 *
416 * Take a reference of the kmod module, incrementing its refcount.
417 *
418 * Returns: the passed @module with its refcount incremented.
419 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200420KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
421{
422 if (mod == NULL)
423 return NULL;
424
425 mod->refcount++;
426
427 return mod;
428}
429
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200430#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
431 do { \
432 if ((_err) < 0) \
433 goto _label_err; \
434 if (*(_list) != NULL) \
435 goto finish; \
436 } while (0)
437
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200438/**
439 * kmod_module_new_from_lookup:
440 * @ctx: kmod library context
441 * @given_alias: alias to look for
442 * @list: an empty list where to save the list of modules matching
443 * @given_alias
444 *
445 * Create a new list of kmod modules using an alias or module name and lookup
446 * libkmod's configuration files and indexes in order to find the module.
447 * Once it's found in one of the places, it stops searching and create the
448 * list of modules that is saved in @list.
449 *
450 * The search order is: 1. aliases in configuration file; 2. module names in
451 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
452 * in modules.alias index.
453 *
454 * The initial refcount is 1, and needs to be decremented to release the
455 * resources of the kmod_module. The returned @list must be released by
456 * calling kmod_module_unref_list(). Since libkmod keeps track of all
457 * kmod_modules created, they are all released upon @ctx destruction too. Do
458 * not unref @ctx before all the desired operations with the returned list are
459 * completed.
460 *
461 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
462 * methods failed, which is basically due to memory allocation fail. If module
463 * is not found, it still returns 0, but @list is an empty list.
464 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200465KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200466 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200467 struct kmod_list **list)
468{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200469 int err;
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200470 char alias[NAME_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200471
Lucas De Marchi4308b172011-12-13 10:26:04 -0200472 if (ctx == NULL || given_alias == NULL)
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200473 return -ENOENT;
474
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200475 if (list == NULL || *list != NULL) {
476 ERR(ctx, "An empty list is needed to create lookup\n");
477 return -ENOSYS;
478 }
479
Lucas De Marchid470db12011-12-13 10:28:00 -0200480 if (alias_normalize(given_alias, alias, NULL) < 0)
481 return -EINVAL;
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200482
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200483 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200484 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200485 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200486
Lucas De Marchi64700e42011-12-01 15:57:53 -0200487 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
488 CHECK_ERR_AND_FINISH(err, fail, list, finish);
489
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200490 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
491 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200492
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200493 err = kmod_lookup_alias_from_commands(ctx, alias, list);
494 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200495
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200496 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
497 CHECK_ERR_AND_FINISH(err, fail, list, finish);
498
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200499finish:
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200500
501 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200502fail:
503 kmod_module_unref_list(*list);
504 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200505 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200506}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200507#undef CHECK_ERR_AND_FINISH
508
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200509/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200510 * kmod_module_unref_list:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200511 * @list: list of kmod modules
512 *
513 * Drop a reference of each kmod module in @list and releases the resources
514 * taken by the list itself.
515 *
516 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
517 * returns the passed @mod with its refcount decremented.
518 */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200519KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
520{
521 for (; list != NULL; list = kmod_list_remove(list))
522 kmod_module_unref(list->data);
523
524 return 0;
525}
526
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200527/**
Lucas De Marchi91428ae2011-12-15 13:09:46 -0200528 * kmod_module_get_dependencies:
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200529 * @mod: kmod module
530 *
531 * Search the modules.dep index to find the dependencies of the given @mod.
532 * The result is cached in @mod, so subsequent calls to this function will
533 * return the already searched list of modules.
534 *
535 * Returns: NULL on failure or if there are any dependencies. Otherwise it
536 * returns a list of kmod modules that can be released by calling
537 * kmod_module_unref_list().
538 */
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200539KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200540{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200541 struct kmod_list *l, *l_new, *list_new = NULL;
542
543 if (mod == NULL)
544 return NULL;
545
Lucas De Marchi671d4892011-12-05 20:23:05 -0200546 if (!mod->init.dep) {
547 /* lazy init */
548 char *line = kmod_search_moddep(mod->ctx, mod->name);
549
550 if (line == NULL)
551 return NULL;
552
553 kmod_module_parse_depline((struct kmod_module *)mod, line);
554 free(line);
555
556 if (!mod->init.dep)
557 return NULL;
558 }
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200559
560 kmod_list_foreach(l, mod->dep) {
561 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
562 if (l_new == NULL) {
563 kmod_module_unref(l->data);
564 goto fail;
565 }
566
567 list_new = l_new;
568 }
569
570 return list_new;
571
572fail:
573 ERR(mod->ctx, "out of memory\n");
574 kmod_module_unref_list(list_new);
575 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200576}
577
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200578/**
579 * kmod_module_get_module:
580 * @entry: an entry in a list of kmod modules.
581 *
582 * Get the kmod module of this @entry in the list, increasing its refcount.
583 * After it's used, unref it. Since the refcount is incremented upon return,
584 * you still have to call kmod_module_unref_list() to release the list of kmod
585 * modules.
586 *
587 * Returns: NULL on failure or the kmod_module contained in this list entry
588 * with its refcount incremented.
589 */
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200590KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200591{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200592 if (entry == NULL)
593 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200594
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200595 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200596}
597
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200598/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200599 * kmod_module_get_name:
600 * @mod: kmod module
601 *
602 * Get the name of this kmod module. Name is always available, independently
603 * if it was created by kmod_module_new_from_name() or another function and
604 * it's always normalized (dashes are replaced with underscores).
605 *
606 * Returns: the name of this kmod module.
607 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200608KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200609{
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200610 if (mod == NULL)
611 return NULL;
612
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200613 return mod->name;
614}
615
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200616/**
617 * kmod_module_get_path:
618 * @mod: kmod module
619 *
620 * Get the path of this kmod module. If this kmod module was not created by
621 * path, it can search the modules.dep index in order to find out the module
622 * under context's dirname (see kmod_get_dirname()).
623 *
624 * Returns: the path of this kmod module or NULL if such information is not
625 * available.
626 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200627KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200628{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200629 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200630
Lucas De Marchi818f8e82011-12-15 13:35:40 -0200631 if (mod == NULL)
632 return NULL;
633
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200634 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200635
Lucas De Marchie005fac2011-12-08 10:42:34 -0200636 if (mod->path != NULL)
637 return mod->path;
638 if (mod->init.dep)
639 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200640
Lucas De Marchie005fac2011-12-08 10:42:34 -0200641 /* lazy init */
642 line = kmod_search_moddep(mod->ctx, mod->name);
643 if (line == NULL)
644 return NULL;
645
646 kmod_module_parse_depline((struct kmod_module *) mod, line);
647 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200648
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200649 return mod->path;
650}
651
652
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200653extern long delete_module(const char *name, unsigned int flags);
654
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200655/**
656 * kmod_module_remove_module:
657 * @mod: kmod module
658 * @flags: flags to pass to Linux kernel when removing the module
659 *
660 * Remove a module from Linux kernel.
661 *
662 * Returns: 0 on success or < 0 on failure.
663 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200664KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
665 unsigned int flags)
666{
667 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200668
669 if (mod == NULL)
670 return -ENOENT;
671
672 /* Filter out other flags */
673 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
674
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200675 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200676 if (err != 0) {
Lucas De Marchi63af0612011-12-14 12:07:37 -0200677 ERR(mod->ctx, "Could not remove '%s': %s\n", mod->name,
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200678 strerror(-err));
679 return err;
680 }
681
682 return 0;
683}
684
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200685extern long init_module(const void *mem, unsigned long len, const char *args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200686
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200687/**
688 * kmod_module_insert_module:
689 * @mod: kmod module
690 * @flags: flags are not passed to Linux Kernel, but instead it dictates the
691 * behavior of this function. They are not implemented yet.
692 * @options: module's options to pass to Linux Kernel.
693 *
694 * Insert a module in Linux kernel. It opens the file pointed by @mod,
695 * mmap'ing it and passing to kernel.
696 *
697 * Returns: 0 on success or < 0 on failure.
698 */
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200699KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200700 unsigned int flags,
701 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200702{
703 int err;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200704 const void *mem;
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200705 off_t size;
706 struct kmod_file *file;
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200707 struct kmod_elf *elf = NULL;
708 const char *path;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200709 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200710
711 if (mod == NULL)
712 return -ENOENT;
713
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200714 path = kmod_module_get_path(mod);
715 if (path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200716 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200717 return -ENOSYS;
718 }
719
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200720 file = kmod_file_open(path);
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200721 if (file == NULL) {
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200722 err = -errno;
723 return err;
724 }
725
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200726 size = kmod_file_get_size(file);
727 mem = kmod_file_get_contents(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200728
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200729 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
730 elf = kmod_elf_new(mem, size);
731 if (elf == NULL) {
732 err = -errno;
733 goto elf_failed;
734 }
735
736 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
737 err = kmod_elf_strip_section(elf, "__versions");
738 if (err < 0)
739 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
740 }
741
742 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
743 err = kmod_elf_strip_vermagic(elf);
744 if (err < 0)
745 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
746 }
747
748 mem = kmod_elf_get_memory(elf);
749 }
750
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200751 err = init_module(mem, size, args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200752 if (err < 0)
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200753 ERR(mod->ctx, "Failed to insert module '%s'\n", path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200754
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -0200755 if (elf != NULL)
756 kmod_elf_unref(elf);
757elf_failed:
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200758 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200759
760 return err;
761}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200762
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200763/**
764 * kmod_module_get_options:
765 * @mod: kmod module
766 *
767 * Get options of this kmod module. Options come from the configuration file
768 * and are cached in @mod. The first call to this function will search for
769 * this module in configuration and subsequent calls return the cached string.
770 *
771 * Returns: a string with all the options separated by spaces. This string is
772 * owned by @mod, do not free it.
773 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200774KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
775{
776 if (mod == NULL)
777 return NULL;
778
779 if (!mod->init.options) {
780 /* lazy init */
781 struct kmod_module *m = (struct kmod_module *)mod;
782 const struct kmod_list *l, *ctx_options;
783 char *opts = NULL;
784 size_t optslen = 0;
785
786 ctx_options = kmod_get_options(mod->ctx);
787
788 kmod_list_foreach(l, ctx_options) {
789 const char *modname = kmod_option_get_modname(l);
790 const char *str;
791 size_t len;
792 void *tmp;
793
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200794 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
795 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
796 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200797 continue;
798
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200799 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 -0200800 str = kmod_option_get_options(l);
801 len = strlen(str);
802 if (len < 1)
803 continue;
804
805 tmp = realloc(opts, optslen + len + 2);
806 if (tmp == NULL) {
807 free(opts);
808 goto failed;
809 }
810
811 opts = tmp;
812
813 if (optslen > 0) {
814 opts[optslen] = ' ';
815 optslen++;
816 }
817
818 memcpy(opts + optslen, str, len);
819 optslen += len;
820 opts[optslen] = '\0';
821 }
822
823 m->init.options = true;
824 m->options = opts;
825 }
826
827 return mod->options;
828
829failed:
830 ERR(mod->ctx, "out of memory\n");
831 return NULL;
832}
833
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200834/**
835 * kmod_module_get_install_commands:
836 * @mod: kmod module
837 *
838 * Get install commands for this kmod module. Install commands come from the
839 * configuration file and are cached in @mod. The first call to this function
840 * will search for this module in configuration and subsequent calls return
841 * the cached string. The install commands are returned as they were in the
842 * configuration, concatenated by ';'. No other processing is made in this
843 * string.
844 *
845 * Returns: a string with all install commands separated by semicolons. This
846 * string is owned by @mod, do not free it.
847 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200848KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
849{
850 if (mod == NULL)
851 return NULL;
852
853 if (!mod->init.install_commands) {
854 /* lazy init */
855 struct kmod_module *m = (struct kmod_module *)mod;
856 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200857
858 ctx_install_commands = kmod_get_install_commands(mod->ctx);
859
860 kmod_list_foreach(l, ctx_install_commands) {
861 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200862
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -0200863 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200864 continue;
865
Lucas De Marchi60f67602011-12-16 03:33:26 -0200866 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200867
Lucas De Marchi60f67602011-12-16 03:33:26 -0200868 /*
869 * find only the first command, as modprobe from
870 * module-init-tools does
871 */
872 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200873 }
874
875 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200876 }
877
878 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200879}
880
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200881void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
882{
883 mod->init.install_commands = true;
884 mod->install_commands = cmd;
885}
886
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200887static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
888{
889 struct kmod_list *ret = NULL;
890 unsigned i;
891
892 for (i = 0; i < count; i++) {
893 const char *depname = array[i];
894 struct kmod_list *lst = NULL;
895 int err;
896
897 err = kmod_module_new_from_lookup(ctx, depname, &lst);
898 if (err < 0) {
899 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
900 continue;
901 } else if (lst != NULL)
902 ret = kmod_list_append_list(ret, lst);
903 }
904 return ret;
905}
906
907/**
908 * kmod_module_get_softdeps:
909 * @mod: kmod module
910 * @pre: where to save the list of preceding soft dependencies.
911 * @post: where to save the list of post soft dependencies.
912 *
913 * Get soft dependencies for this kmod module. Soft dependencies come
914 * from configuration file and are cached in @mod. The first call
915 * to this function will search for this module in configuration and
916 * subsequent calls return the known results.
917 *
918 * Both @pre and @post are newly created list of kmod_module and
919 * should be unreferenced with kmod_module_unref_list().
920 *
921 * Returns: 0 on success or < 0 otherwise.
922 */
923KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod, struct kmod_list **pre, struct kmod_list **post)
924{
925 const struct kmod_list *l;
926 struct kmod_list *l_new;
927
928 if (mod == NULL || pre == NULL || post == NULL)
929 return -ENOENT;
930
931 assert(*pre == NULL);
932 assert(*post == NULL);
933
934 if (!mod->init.softdeps) {
935 /* lazy init */
936 struct kmod_module *m = (struct kmod_module *)mod;
937 const struct kmod_list *ctx_softdeps;
938
939 ctx_softdeps = kmod_get_softdeps(mod->ctx);
940
941 kmod_list_foreach(l, ctx_softdeps) {
942 const char *modname = kmod_softdep_get_name(l);
943 const char * const *array;
944 unsigned count;
945
946 if (fnmatch(modname, mod->name, 0) != 0)
947 continue;
948
949 array = kmod_softdep_get_pre(l, &count);
950 m->softdeps.pre = lookup_softdep(mod->ctx, array, count);
951 array = kmod_softdep_get_post(l, &count);
952 m->softdeps.post = lookup_softdep(mod->ctx, array, count);
953 /*
954 * find only the first command, as modprobe from
955 * module-init-tools does
956 */
957 break;
958 }
959
960 m->init.softdeps = true;
961 }
962
963 kmod_list_foreach(l, mod->softdeps.pre) {
964 l_new = kmod_list_append(*pre, kmod_module_ref(l->data));
965 if (l_new == NULL) {
966 kmod_module_unref(l->data);
967 goto fail;
968 }
969 *pre = l_new;
970 }
971
972 kmod_list_foreach(l, mod->softdeps.post) {
973 l_new = kmod_list_append(*post, kmod_module_ref(l->data));
974 if (l_new == NULL) {
975 kmod_module_unref(l->data);
976 goto fail;
977 }
978 *post = l_new;
979 }
980
981 return 0;
982
983fail:
984 kmod_module_unref_list(*pre);
985 *pre = NULL;
986 kmod_module_unref_list(*post);
987 *post = NULL;
988 return -ENOMEM;
989}
990
991
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200992/**
993 * kmod_module_get_remove_commands:
994 * @mod: kmod module
995 *
996 * Get remove commands for this kmod module. Remove commands come from the
997 * configuration file and are cached in @mod. The first call to this function
998 * will search for this module in configuration and subsequent calls return
999 * the cached string. The remove commands are returned as they were in the
1000 * configuration, concatenated by ';'. No other processing is made in this
1001 * string.
1002 *
1003 * Returns: a string with all remove commands separated by semicolons. This
1004 * string is owned by @mod, do not free it.
1005 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001006KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1007{
1008 if (mod == NULL)
1009 return NULL;
1010
1011 if (!mod->init.remove_commands) {
1012 /* lazy init */
1013 struct kmod_module *m = (struct kmod_module *)mod;
1014 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001015
1016 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1017
1018 kmod_list_foreach(l, ctx_remove_commands) {
1019 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001020
Gustavo Sverzut Barbieria6bf2492011-12-16 22:43:04 -02001021 if (fnmatch(modname, mod->name, 0) != 0)
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001022 continue;
1023
Lucas De Marchi60f67602011-12-16 03:33:26 -02001024 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001025
Lucas De Marchi60f67602011-12-16 03:33:26 -02001026 /*
1027 * find only the first command, as modprobe from
1028 * module-init-tools does
1029 */
1030 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001031 }
1032
1033 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001034 }
1035
1036 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001037}
1038
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001039void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1040{
1041 mod->init.remove_commands = true;
1042 mod->remove_commands = cmd;
1043}
1044
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001045/**
1046 * SECTION:libkmod-loaded
1047 * @short_description: currently loaded modules
1048 *
1049 * Information about currently loaded modules, as reported by Linux kernel.
1050 * These information are not cached by libkmod and are always read from /sys
1051 * and /proc/modules.
1052 */
1053
1054/**
1055 * kmod_module_new_from_loaded:
1056 * @ctx: kmod library context
1057 * @list: where to save the list of loaded modules
1058 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001059 * Create a new list of kmod modules with all modules currently loaded in
1060 * kernel. It uses /proc/modules to get the names of loaded modules and to
1061 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1062 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001063 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001064 * The initial refcount is 1, and needs to be decremented to release the
1065 * resources of the kmod_module. The returned @list must be released by
1066 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1067 * kmod_modules created, they are all released upon @ctx destruction too. Do
1068 * not unref @ctx before all the desired operations with the returned list are
1069 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001070 *
1071 * Returns: 0 on success or < 0 on error.
1072 */
1073KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1074 struct kmod_list **list)
1075{
1076 struct kmod_list *l = NULL;
1077 FILE *fp;
1078 char line[4096];
1079
1080 if (ctx == NULL || list == NULL)
1081 return -ENOENT;
1082
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001083 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001084 if (fp == NULL) {
1085 int err = -errno;
1086 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1087 return err;
1088 }
1089
1090 while (fgets(line, sizeof(line), fp)) {
1091 struct kmod_module *m;
1092 struct kmod_list *node;
1093 int err;
1094 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1095
1096 err = kmod_module_new_from_name(ctx, name, &m);
1097 if (err < 0) {
1098 ERR(ctx, "could not get module from name '%s': %s\n",
1099 name, strerror(-err));
1100 continue;
1101 }
1102
1103 node = kmod_list_append(l, m);
1104 if (node)
1105 l = node;
1106 else {
1107 ERR(ctx, "out of memory\n");
1108 kmod_module_unref(m);
1109 }
1110 }
1111
1112 fclose(fp);
1113 *list = l;
1114
1115 return 0;
1116}
1117
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001118/**
1119 * kmod_module_initstate_str:
1120 * @state: the state as returned by kmod_module_get_initstate()
1121 *
1122 * Translate a initstate to a string.
1123 *
1124 * Returns: the string associated to the @state. This string is statically
1125 * allocated, do not free it.
1126 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001127KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1128{
1129 switch (state) {
1130 case KMOD_MODULE_BUILTIN:
1131 return "builtin";
1132 case KMOD_MODULE_LIVE:
1133 return "live";
1134 case KMOD_MODULE_COMING:
1135 return "coming";
1136 case KMOD_MODULE_GOING:
1137 return "going";
1138 default:
1139 return NULL;
1140 }
1141}
1142
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001143/**
1144 * kmod_module_get_initstate:
1145 * @mod: kmod module
1146 *
1147 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1148 * /sys filesystem.
1149 *
1150 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1151 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001152KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1153{
1154 char path[PATH_MAX], buf[32];
1155 int fd, err, pathlen;
1156
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001157 if (mod == NULL)
1158 return -ENOENT;
1159
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001160 pathlen = snprintf(path, sizeof(path),
1161 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001162 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001163 if (fd < 0) {
1164 err = -errno;
1165
1166 if (pathlen > (int)sizeof("/initstate") - 1) {
1167 struct stat st;
1168 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1169 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1170 return KMOD_MODULE_BUILTIN;
1171 }
1172
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001173 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001174 path, strerror(-err));
1175 return err;
1176 }
1177
1178 err = read_str_safe(fd, buf, sizeof(buf));
1179 close(fd);
1180 if (err < 0) {
1181 ERR(mod->ctx, "could not read from '%s': %s\n",
1182 path, strerror(-err));
1183 return err;
1184 }
1185
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001186 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001187 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001188 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001189 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001190 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001191 return KMOD_MODULE_GOING;
1192
1193 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1194 return -EINVAL;
1195}
1196
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001197/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001198 * kmod_module_get_size:
1199 * @mod: kmod module
1200 *
1201 * Get the size of this kmod module as returned by Linux kernel. It reads the
1202 * file /proc/modules to search for this module and get its size.
1203 *
1204 * Returns: the size of this kmod module.
1205 */
1206KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1207{
1208 // FIXME TODO: this should be available from /sys/module/foo
1209 FILE *fp;
1210 char line[4096];
1211 int lineno = 0;
1212 long size = -ENOENT;
1213
1214 if (mod == NULL)
1215 return -ENOENT;
1216
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001217 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001218 if (fp == NULL) {
1219 int err = -errno;
1220 ERR(mod->ctx,
1221 "could not open /proc/modules: %s\n", strerror(errno));
1222 return err;
1223 }
1224
1225 while (fgets(line, sizeof(line), fp)) {
1226 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1227 long value;
1228
1229 lineno++;
1230 if (tok == NULL || !streq(tok, mod->name))
1231 continue;
1232
1233 tok = strtok_r(NULL, " \t", &saveptr);
1234 if (tok == NULL) {
1235 ERR(mod->ctx,
1236 "invalid line format at /proc/modules:%d\n", lineno);
1237 break;
1238 }
1239
1240 value = strtol(tok, &endptr, 10);
1241 if (endptr == tok || *endptr != '\0') {
1242 ERR(mod->ctx,
1243 "invalid line format at /proc/modules:%d\n", lineno);
1244 break;
1245 }
1246
1247 size = value;
1248 break;
1249 }
1250 fclose(fp);
1251 return size;
1252}
1253
1254/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001255 * kmod_module_get_refcnt:
1256 * @mod: kmod module
1257 *
1258 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1259 * /sys filesystem.
1260 *
1261 * Returns: 0 on success or < 0 on failure.
1262 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001263KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1264{
1265 char path[PATH_MAX];
1266 long refcnt;
1267 int fd, err;
1268
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001269 if (mod == NULL)
1270 return -ENOENT;
1271
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001272 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001273 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001274 if (fd < 0) {
1275 err = -errno;
1276 ERR(mod->ctx, "could not open '%s': %s\n",
1277 path, strerror(errno));
1278 return err;
1279 }
1280
1281 err = read_str_long(fd, &refcnt, 10);
1282 close(fd);
1283 if (err < 0) {
1284 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1285 path, strerror(-err));
1286 return err;
1287 }
1288
1289 return (int)refcnt;
1290}
1291
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001292/**
1293 * kmod_module_get_holders:
1294 * @mod: kmod module
1295 *
1296 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1297 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1298 *
1299 * Returns: a new list of kmod modules on success or NULL on failure.
1300 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001301KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1302{
1303 char dname[PATH_MAX];
1304 struct kmod_list *list = NULL;
1305 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001306
1307 if (mod == NULL)
1308 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001309
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001310 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1311
1312 d = opendir(dname);
1313 if (d == NULL) {
1314 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001315 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001316 return NULL;
1317 }
1318
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001319 for (;;) {
1320 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001321 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001322 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001323 int err;
1324
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001325 err = readdir_r(d, &de, &entp);
1326 if (err != 0) {
1327 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1328 mod->name, strerror(-err));
1329 goto fail;
1330 }
1331
1332 if (entp == NULL)
1333 break;
1334
1335 if (de.d_name[0] == '.') {
1336 if (de.d_name[1] == '\0' ||
1337 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001338 continue;
1339 }
1340
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001341 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001342 if (err < 0) {
1343 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001344 de.d_name, strerror(-err));
1345 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001346 }
1347
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001348 l = kmod_list_append(list, holder);
1349 if (l != NULL) {
1350 list = l;
1351 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001352 ERR(mod->ctx, "out of memory\n");
1353 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001354 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001355 }
1356 }
1357
1358 closedir(d);
1359 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001360
1361fail:
1362 closedir(d);
1363 kmod_module_unref_list(list);
1364 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001365}
1366
1367struct kmod_module_section {
1368 unsigned long address;
1369 char name[];
1370};
1371
1372static void kmod_module_section_free(struct kmod_module_section *section)
1373{
1374 free(section);
1375}
1376
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001377/**
1378 * kmod_module_get_sections:
1379 * @mod: kmod module
1380 *
1381 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1382 * structure contained in this list is internal to libkmod and their fields
1383 * can be obtained by calling kmod_module_section_get_name() and
1384 * kmod_module_section_get_address().
1385 *
1386 * After use, free the @list by calling kmod_module_section_free_list().
1387 *
1388 * Returns: a new list of kmod module sections on success or NULL on failure.
1389 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001390KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1391{
1392 char dname[PATH_MAX];
1393 struct kmod_list *list = NULL;
1394 DIR *d;
1395 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001396
1397 if (mod == NULL)
1398 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001399
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001400 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1401
1402 d = opendir(dname);
1403 if (d == NULL) {
1404 ERR(mod->ctx, "could not open '%s': %s\n",
1405 dname, strerror(errno));
1406 return NULL;
1407 }
1408
1409 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001410
1411 for (;;) {
1412 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001413 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001414 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001415 unsigned long address;
1416 size_t namesz;
1417 int fd, err;
1418
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001419 err = readdir_r(d, &de, &entp);
1420 if (err != 0) {
1421 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1422 mod->name, strerror(-err));
1423 goto fail;
1424 }
1425
1426 if (de.d_name[0] == '.') {
1427 if (de.d_name[1] == '\0' ||
1428 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001429 continue;
1430 }
1431
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001432 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001433 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001434 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1435 dname, de.d_name);
1436 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001437 }
1438
1439 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001440 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001441 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001442 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1443 dname, de.d_name);
1444 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001445 }
1446
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001447 namesz = strlen(de.d_name) + 1;
1448 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001449
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001450 if (section == NULL) {
1451 ERR(mod->ctx, "out of memory\n");
1452 goto fail;
1453 }
1454
1455 section->address = address;
1456 memcpy(section->name, de.d_name, namesz);
1457
1458 l = kmod_list_append(list, section);
1459 if (l != NULL) {
1460 list = l;
1461 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001462 ERR(mod->ctx, "out of memory\n");
1463 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001464 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001465 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001466 }
1467
1468 closedir(d);
1469 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001470
1471fail:
1472 closedir(d);
1473 kmod_module_unref_list(list);
1474 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001475}
1476
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001477/**
1478 * kmod_module_section_get_module_name:
1479 * @entry: a list entry representing a kmod module section
1480 *
1481 * Get the name of a kmod module section.
1482 *
1483 * After use, free the @list by calling kmod_module_section_free_list().
1484 *
1485 * Returns: the name of this kmod module section on success or NULL on
1486 * failure. The string is owned by the section, do not free it.
1487 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001488KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1489{
1490 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001491
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001492 if (entry == NULL)
1493 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001494
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001495 section = entry->data;
1496 return section->name;
1497}
1498
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001499/**
1500 * kmod_module_section_get_address:
1501 * @entry: a list entry representing a kmod module section
1502 *
1503 * Get the address of a kmod module section.
1504 *
1505 * After use, free the @list by calling kmod_module_section_free_list().
1506 *
1507 * Returns: the address of this kmod module section on success or ULONG_MAX
1508 * on failure.
1509 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001510KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1511{
1512 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001513
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001514 if (entry == NULL)
1515 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001516
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001517 section = entry->data;
1518 return section->address;
1519}
1520
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001521/**
1522 * kmod_module_section_free_list:
1523 * @list: kmod module section list
1524 *
1525 * Release the resources taken by @list
1526 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001527KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1528{
1529 while (list) {
1530 kmod_module_section_free(list->data);
1531 list = kmod_list_remove(list);
1532 }
1533}
Gustavo Sverzut Barbieri708624a2011-12-18 01:25:06 -02001534
1535struct kmod_module_info {
1536 char *key;
1537 char value[];
1538};
1539
1540static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
1541{
1542 struct kmod_module_info *info;
1543
1544 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
1545 if (info == NULL)
1546 return NULL;
1547
1548 info->key = (char *)info + sizeof(struct kmod_module_info)
1549 + valuelen + 1;
1550 memcpy(info->key, key, keylen);
1551 info->key[keylen] = '\0';
1552 memcpy(info->value, value, valuelen);
1553 info->value[valuelen] = '\0';
1554 return info;
1555}
1556
1557static void kmod_module_info_free(struct kmod_module_info *info)
1558{
1559 free(info);
1560}
1561
1562/**
1563 * kmod_module_get_info:
1564 * @mod: kmod module
1565 * @list: where to return list of module information. Use
1566 * kmod_module_info_get_key() and
1567 * kmod_module_info_get_value(). Release this list with
1568 * kmod_module_info_unref_list()
1569 *
1570 * Get a list of entries in ELF section ".modinfo", these contain
1571 * alias, license, depends, vermagic and other keys with respective
1572 * values.
1573 *
1574 * After use, free the @list by calling kmod_module_info_free_list().
1575 *
1576 * Returns: 0 on success or < 0 otherwise.
1577 */
1578KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
1579{
1580 struct kmod_file *file;
1581 struct kmod_elf *elf;
1582 const char *path;
1583 const void *mem;
1584 char **strings;
1585 size_t size;
1586 int i, count, ret = 0;
1587
1588 if (mod == NULL || list == NULL)
1589 return -ENOENT;
1590
1591 assert(*list == NULL);
1592
1593 path = kmod_module_get_path(mod);
1594 if (path == NULL)
1595 return -ENOENT;
1596
1597 file = kmod_file_open(path);
1598 if (file == NULL)
1599 return -errno;
1600
1601 size = kmod_file_get_size(file);
1602 mem = kmod_file_get_contents(file);
1603
1604 elf = kmod_elf_new(mem, size);
1605 if (elf == NULL) {
1606 ret = -errno;
1607 goto elf_open_error;
1608 }
1609
1610 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
1611 if (count < 0) {
1612 ret = count;
1613 goto get_strings_error;
1614 }
1615
1616 for (i = 0; i < count; i++) {
1617 struct kmod_module_info *info;
1618 struct kmod_list *n;
1619 const char *key, *value;
1620 size_t keylen, valuelen;
1621
1622 key = strings[i];
1623 value = strchr(key, '=');
1624 if (value == NULL) {
1625 keylen = strlen(key);
1626 valuelen = 0;
1627 } else {
1628 keylen = value - key;
1629 value++;
1630 valuelen = strlen(value);
1631 }
1632
1633 info = kmod_module_info_new(key, keylen, value, valuelen);
1634 if (info == NULL) {
1635 ret = -errno;
1636 kmod_module_info_free_list(*list);
1637 *list = NULL;
1638 goto list_error;
1639 }
1640
1641 n = kmod_list_append(*list, info);
1642 if (n != NULL)
1643 *list = n;
1644 else {
1645 kmod_module_info_free(info);
1646 kmod_module_info_free_list(*list);
1647 *list = NULL;
1648 ret = -ENOMEM;
1649 goto list_error;
1650 }
1651 }
1652 ret = count;
1653
1654list_error:
1655 free(strings);
1656get_strings_error:
1657 kmod_elf_unref(elf);
1658elf_open_error:
1659 kmod_file_unref(file);
1660
1661 return ret;
1662}
1663
1664/**
1665 * kmod_module_info_get_key:
1666 * @entry: a list entry representing a kmod module info
1667 *
1668 * Get the key of a kmod module info.
1669 *
1670 * Returns: the key of this kmod module info on success or NULL on
1671 * failure. The string is owned by the info, do not free it.
1672 */
1673KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
1674{
1675 struct kmod_module_info *info;
1676
1677 if (entry == NULL)
1678 return NULL;
1679
1680 info = entry->data;
1681 return info->key;
1682}
1683
1684/**
1685 * kmod_module_info_get_value:
1686 * @entry: a list entry representing a kmod module info
1687 *
1688 * Get the value of a kmod module info.
1689 *
1690 * Returns: the value of this kmod module info on success or NULL on
1691 * failure. The string is owned by the info, do not free it.
1692 */
1693KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
1694{
1695 struct kmod_module_info *info;
1696
1697 if (entry == NULL)
1698 return NULL;
1699
1700 info = entry->data;
1701 return info->value;
1702}
1703
1704/**
1705 * kmod_module_info_free_list:
1706 * @list: kmod module info list
1707 *
1708 * Release the resources taken by @list
1709 */
1710KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
1711{
1712 while (list) {
1713 kmod_module_info_free(list->data);
1714 list = kmod_list_remove(list);
1715 }
1716}
1717
1718struct kmod_module_version {
1719 uint64_t crc;
1720 char symbol[];
1721};
1722
1723static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
1724{
1725 struct kmod_module_version *mv;
1726 size_t symbollen = strlen(symbol) + 1;
1727
1728 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
1729 if (mv == NULL)
1730 return NULL;
1731
1732 mv->crc = crc;
1733 memcpy(mv->symbol, symbol, symbollen);
1734 return mv;
1735}
1736
1737static void kmod_module_version_free(struct kmod_module_version *version)
1738{
1739 free(version);
1740}
1741
1742/**
1743 * kmod_module_get_versions:
1744 * @mod: kmod module
1745 * @list: where to return list of module versions. Use
1746 * kmod_module_versions_get_symbol() and
1747 * kmod_module_versions_get_crc(). Release this list with
1748 * kmod_module_versions_unref_list()
1749 *
1750 * Get a list of entries in ELF section "__versions".
1751 *
1752 * After use, free the @list by calling kmod_module_versions_free_list().
1753 *
1754 * Returns: 0 on success or < 0 otherwise.
1755 */
1756KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
1757{
1758 struct kmod_file *file;
1759 struct kmod_elf *elf;
1760 const char *path;
1761 const void *mem;
1762 struct kmod_modversion *versions;
1763 size_t size;
1764 int i, count, ret = 0;
1765
1766 if (mod == NULL || list == NULL)
1767 return -ENOENT;
1768
1769 assert(*list == NULL);
1770
1771 path = kmod_module_get_path(mod);
1772 if (path == NULL)
1773 return -ENOENT;
1774
1775 file = kmod_file_open(path);
1776 if (file == NULL)
1777 return -errno;
1778
1779 size = kmod_file_get_size(file);
1780 mem = kmod_file_get_contents(file);
1781
1782 elf = kmod_elf_new(mem, size);
1783 if (elf == NULL) {
1784 ret = -errno;
1785 goto elf_open_error;
1786 }
1787
1788 count = kmod_elf_get_modversions(elf, &versions);
1789 if (count < 0) {
1790 ret = count;
1791 goto get_strings_error;
1792 }
1793
1794 for (i = 0; i < count; i++) {
1795 struct kmod_module_version *mv;
1796 struct kmod_list *n;
1797
1798 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
1799 if (mv == NULL) {
1800 ret = -errno;
1801 kmod_module_versions_free_list(*list);
1802 *list = NULL;
1803 goto list_error;
1804 }
1805
1806 n = kmod_list_append(*list, mv);
1807 if (n != NULL)
1808 *list = n;
1809 else {
1810 kmod_module_version_free(mv);
1811 kmod_module_versions_free_list(*list);
1812 *list = NULL;
1813 ret = -ENOMEM;
1814 goto list_error;
1815 }
1816 }
1817 ret = count;
1818
1819list_error:
1820 free(versions);
1821get_strings_error:
1822 kmod_elf_unref(elf);
1823elf_open_error:
1824 kmod_file_unref(file);
1825
1826 return ret;
1827}
1828
1829/**
1830 * kmod_module_versions_get_symbol:
1831 * @entry: a list entry representing a kmod module versions
1832 *
1833 * Get the symbol of a kmod module versions.
1834 *
1835 * Returns: the symbol of this kmod module versions on success or NULL
1836 * on failure. The string is owned by the versions, do not free it.
1837 */
1838KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
1839{
1840 struct kmod_module_version *version;
1841
1842 if (entry == NULL)
1843 return NULL;
1844
1845 version = entry->data;
1846 return version->symbol;
1847}
1848
1849/**
1850 * kmod_module_version_get_crc:
1851 * @entry: a list entry representing a kmod module version
1852 *
1853 * Get the crc of a kmod module version.
1854 *
1855 * Returns: the crc of this kmod module version on success or NULL on
1856 * failure. The string is owned by the version, do not free it.
1857 */
1858KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
1859{
1860 struct kmod_module_version *version;
1861
1862 if (entry == NULL)
1863 return 0;
1864
1865 version = entry->data;
1866 return version->crc;
1867}
1868
1869/**
1870 * kmod_module_versions_free_list:
1871 * @list: kmod module versions list
1872 *
1873 * Release the resources taken by @list
1874 */
1875KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
1876{
1877 while (list) {
1878 kmod_module_version_free(list->data);
1879 list = kmod_list_remove(list);
1880 }
1881}