blob: cb0ec3dc6a7778c4f443ac3446b3b11db37a3809 [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 Marchid753b8c2011-12-05 18:14:51 -0200369 memcpy(m->name, name, namelen);
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
685extern long init_module(void *mem, unsigned long len, const char *args);
686
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 Barbieri3d8226e2011-12-16 16:08:53 -0200704 void *mem;
705 off_t size;
706 struct kmod_file *file;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200707 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200708
709 if (mod == NULL)
710 return -ENOENT;
711
712 if (mod->path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200713 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200714 return -ENOSYS;
715 }
716
717 if (flags != 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200718 INFO(mod->ctx, "Flags are not implemented yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200719
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200720 file = kmod_file_open(mod->path);
721 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 Barbieri3d8226e2011-12-16 16:08:53 -0200729 err = init_module(mem, size, args);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200730 if (err < 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200731 ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200732
Gustavo Sverzut Barbieri3d8226e2011-12-16 16:08:53 -0200733 kmod_file_unref(file);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200734
735 return err;
736}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200737
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200738/**
739 * kmod_module_get_options:
740 * @mod: kmod module
741 *
742 * Get options of this kmod module. Options come from the configuration file
743 * and are cached in @mod. The first call to this function will search for
744 * this module in configuration and subsequent calls return the cached string.
745 *
746 * Returns: a string with all the options separated by spaces. This string is
747 * owned by @mod, do not free it.
748 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200749KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
750{
751 if (mod == NULL)
752 return NULL;
753
754 if (!mod->init.options) {
755 /* lazy init */
756 struct kmod_module *m = (struct kmod_module *)mod;
757 const struct kmod_list *l, *ctx_options;
758 char *opts = NULL;
759 size_t optslen = 0;
760
761 ctx_options = kmod_get_options(mod->ctx);
762
763 kmod_list_foreach(l, ctx_options) {
764 const char *modname = kmod_option_get_modname(l);
765 const char *str;
766 size_t len;
767 void *tmp;
768
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200769 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
770 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
771 streq(modname, mod->alias))))
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200772 continue;
773
Lucas De Marchi07b8c822011-12-13 14:21:24 -0200774 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 -0200775 str = kmod_option_get_options(l);
776 len = strlen(str);
777 if (len < 1)
778 continue;
779
780 tmp = realloc(opts, optslen + len + 2);
781 if (tmp == NULL) {
782 free(opts);
783 goto failed;
784 }
785
786 opts = tmp;
787
788 if (optslen > 0) {
789 opts[optslen] = ' ';
790 optslen++;
791 }
792
793 memcpy(opts + optslen, str, len);
794 optslen += len;
795 opts[optslen] = '\0';
796 }
797
798 m->init.options = true;
799 m->options = opts;
800 }
801
802 return mod->options;
803
804failed:
805 ERR(mod->ctx, "out of memory\n");
806 return NULL;
807}
808
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200809/**
810 * kmod_module_get_install_commands:
811 * @mod: kmod module
812 *
813 * Get install commands for this kmod module. Install commands come from the
814 * configuration file and are cached in @mod. The first call to this function
815 * will search for this module in configuration and subsequent calls return
816 * the cached string. The install commands are returned as they were in the
817 * configuration, concatenated by ';'. No other processing is made in this
818 * string.
819 *
820 * Returns: a string with all install commands separated by semicolons. This
821 * string is owned by @mod, do not free it.
822 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200823KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
824{
825 if (mod == NULL)
826 return NULL;
827
828 if (!mod->init.install_commands) {
829 /* lazy init */
830 struct kmod_module *m = (struct kmod_module *)mod;
831 const struct kmod_list *l, *ctx_install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200832
833 ctx_install_commands = kmod_get_install_commands(mod->ctx);
834
835 kmod_list_foreach(l, ctx_install_commands) {
836 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200837
838 if (strcmp(modname, mod->name) != 0)
839 continue;
840
Lucas De Marchi60f67602011-12-16 03:33:26 -0200841 m->install_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200842
Lucas De Marchi60f67602011-12-16 03:33:26 -0200843 /*
844 * find only the first command, as modprobe from
845 * module-init-tools does
846 */
847 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200848 }
849
850 m->init.install_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200851 }
852
853 return mod->install_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200854}
855
Lucas De Marchif4fc5522011-12-16 03:57:12 -0200856void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
857{
858 mod->init.install_commands = true;
859 mod->install_commands = cmd;
860}
861
Gustavo Sverzut Barbieri1c522602011-12-16 21:18:10 -0200862static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
863{
864 struct kmod_list *ret = NULL;
865 unsigned i;
866
867 for (i = 0; i < count; i++) {
868 const char *depname = array[i];
869 struct kmod_list *lst = NULL;
870 int err;
871
872 err = kmod_module_new_from_lookup(ctx, depname, &lst);
873 if (err < 0) {
874 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
875 continue;
876 } else if (lst != NULL)
877 ret = kmod_list_append_list(ret, lst);
878 }
879 return ret;
880}
881
882/**
883 * kmod_module_get_softdeps:
884 * @mod: kmod module
885 * @pre: where to save the list of preceding soft dependencies.
886 * @post: where to save the list of post soft dependencies.
887 *
888 * Get soft dependencies for this kmod module. Soft dependencies come
889 * from configuration file and are cached in @mod. The first call
890 * to this function will search for this module in configuration and
891 * subsequent calls return the known results.
892 *
893 * Both @pre and @post are newly created list of kmod_module and
894 * should be unreferenced with kmod_module_unref_list().
895 *
896 * Returns: 0 on success or < 0 otherwise.
897 */
898KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod, struct kmod_list **pre, struct kmod_list **post)
899{
900 const struct kmod_list *l;
901 struct kmod_list *l_new;
902
903 if (mod == NULL || pre == NULL || post == NULL)
904 return -ENOENT;
905
906 assert(*pre == NULL);
907 assert(*post == NULL);
908
909 if (!mod->init.softdeps) {
910 /* lazy init */
911 struct kmod_module *m = (struct kmod_module *)mod;
912 const struct kmod_list *ctx_softdeps;
913
914 ctx_softdeps = kmod_get_softdeps(mod->ctx);
915
916 kmod_list_foreach(l, ctx_softdeps) {
917 const char *modname = kmod_softdep_get_name(l);
918 const char * const *array;
919 unsigned count;
920
921 if (fnmatch(modname, mod->name, 0) != 0)
922 continue;
923
924 array = kmod_softdep_get_pre(l, &count);
925 m->softdeps.pre = lookup_softdep(mod->ctx, array, count);
926 array = kmod_softdep_get_post(l, &count);
927 m->softdeps.post = lookup_softdep(mod->ctx, array, count);
928 /*
929 * find only the first command, as modprobe from
930 * module-init-tools does
931 */
932 break;
933 }
934
935 m->init.softdeps = true;
936 }
937
938 kmod_list_foreach(l, mod->softdeps.pre) {
939 l_new = kmod_list_append(*pre, kmod_module_ref(l->data));
940 if (l_new == NULL) {
941 kmod_module_unref(l->data);
942 goto fail;
943 }
944 *pre = l_new;
945 }
946
947 kmod_list_foreach(l, mod->softdeps.post) {
948 l_new = kmod_list_append(*post, kmod_module_ref(l->data));
949 if (l_new == NULL) {
950 kmod_module_unref(l->data);
951 goto fail;
952 }
953 *post = l_new;
954 }
955
956 return 0;
957
958fail:
959 kmod_module_unref_list(*pre);
960 *pre = NULL;
961 kmod_module_unref_list(*post);
962 *post = NULL;
963 return -ENOMEM;
964}
965
966
Lucas De Marchi7afc98a2011-12-14 12:06:18 -0200967/**
968 * kmod_module_get_remove_commands:
969 * @mod: kmod module
970 *
971 * Get remove commands for this kmod module. Remove commands come from the
972 * configuration file and are cached in @mod. The first call to this function
973 * will search for this module in configuration and subsequent calls return
974 * the cached string. The remove commands are returned as they were in the
975 * configuration, concatenated by ';'. No other processing is made in this
976 * string.
977 *
978 * Returns: a string with all remove commands separated by semicolons. This
979 * string is owned by @mod, do not free it.
980 */
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200981KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
982{
983 if (mod == NULL)
984 return NULL;
985
986 if (!mod->init.remove_commands) {
987 /* lazy init */
988 struct kmod_module *m = (struct kmod_module *)mod;
989 const struct kmod_list *l, *ctx_remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200990
991 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
992
993 kmod_list_foreach(l, ctx_remove_commands) {
994 const char *modname = kmod_command_get_modname(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200995
996 if (strcmp(modname, mod->name) != 0)
997 continue;
998
Lucas De Marchi60f67602011-12-16 03:33:26 -0200999 m->remove_commands = kmod_command_get_command(l);
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001000
Lucas De Marchi60f67602011-12-16 03:33:26 -02001001 /*
1002 * find only the first command, as modprobe from
1003 * module-init-tools does
1004 */
1005 break;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001006 }
1007
1008 m->init.remove_commands = true;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001009 }
1010
1011 return mod->remove_commands;
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001012}
1013
Lucas De Marchif4fc5522011-12-16 03:57:12 -02001014void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1015{
1016 mod->init.remove_commands = true;
1017 mod->remove_commands = cmd;
1018}
1019
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001020/**
1021 * SECTION:libkmod-loaded
1022 * @short_description: currently loaded modules
1023 *
1024 * Information about currently loaded modules, as reported by Linux kernel.
1025 * These information are not cached by libkmod and are always read from /sys
1026 * and /proc/modules.
1027 */
1028
1029/**
1030 * kmod_module_new_from_loaded:
1031 * @ctx: kmod library context
1032 * @list: where to save the list of loaded modules
1033 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001034 * Create a new list of kmod modules with all modules currently loaded in
1035 * kernel. It uses /proc/modules to get the names of loaded modules and to
1036 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1037 * They are put are put in @list in no particular order.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001038 *
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001039 * The initial refcount is 1, and needs to be decremented to release the
1040 * resources of the kmod_module. The returned @list must be released by
1041 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1042 * kmod_modules created, they are all released upon @ctx destruction too. Do
1043 * not unref @ctx before all the desired operations with the returned list are
1044 * completed.
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001045 *
1046 * Returns: 0 on success or < 0 on error.
1047 */
1048KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1049 struct kmod_list **list)
1050{
1051 struct kmod_list *l = NULL;
1052 FILE *fp;
1053 char line[4096];
1054
1055 if (ctx == NULL || list == NULL)
1056 return -ENOENT;
1057
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001058 fp = fopen("/proc/modules", "re");
Lucas De Marchi49ce6d02011-12-12 13:56:47 -02001059 if (fp == NULL) {
1060 int err = -errno;
1061 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1062 return err;
1063 }
1064
1065 while (fgets(line, sizeof(line), fp)) {
1066 struct kmod_module *m;
1067 struct kmod_list *node;
1068 int err;
1069 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1070
1071 err = kmod_module_new_from_name(ctx, name, &m);
1072 if (err < 0) {
1073 ERR(ctx, "could not get module from name '%s': %s\n",
1074 name, strerror(-err));
1075 continue;
1076 }
1077
1078 node = kmod_list_append(l, m);
1079 if (node)
1080 l = node;
1081 else {
1082 ERR(ctx, "out of memory\n");
1083 kmod_module_unref(m);
1084 }
1085 }
1086
1087 fclose(fp);
1088 *list = l;
1089
1090 return 0;
1091}
1092
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001093/**
1094 * kmod_module_initstate_str:
1095 * @state: the state as returned by kmod_module_get_initstate()
1096 *
1097 * Translate a initstate to a string.
1098 *
1099 * Returns: the string associated to the @state. This string is statically
1100 * allocated, do not free it.
1101 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001102KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1103{
1104 switch (state) {
1105 case KMOD_MODULE_BUILTIN:
1106 return "builtin";
1107 case KMOD_MODULE_LIVE:
1108 return "live";
1109 case KMOD_MODULE_COMING:
1110 return "coming";
1111 case KMOD_MODULE_GOING:
1112 return "going";
1113 default:
1114 return NULL;
1115 }
1116}
1117
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001118/**
1119 * kmod_module_get_initstate:
1120 * @mod: kmod module
1121 *
1122 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1123 * /sys filesystem.
1124 *
1125 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1126 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001127KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1128{
1129 char path[PATH_MAX], buf[32];
1130 int fd, err, pathlen;
1131
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001132 if (mod == NULL)
1133 return -ENOENT;
1134
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001135 pathlen = snprintf(path, sizeof(path),
1136 "/sys/module/%s/initstate", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001137 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001138 if (fd < 0) {
1139 err = -errno;
1140
1141 if (pathlen > (int)sizeof("/initstate") - 1) {
1142 struct stat st;
1143 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1144 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1145 return KMOD_MODULE_BUILTIN;
1146 }
1147
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -02001148 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001149 path, strerror(-err));
1150 return err;
1151 }
1152
1153 err = read_str_safe(fd, buf, sizeof(buf));
1154 close(fd);
1155 if (err < 0) {
1156 ERR(mod->ctx, "could not read from '%s': %s\n",
1157 path, strerror(-err));
1158 return err;
1159 }
1160
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001161 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001162 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001163 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001164 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -02001165 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001166 return KMOD_MODULE_GOING;
1167
1168 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1169 return -EINVAL;
1170}
1171
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001172/**
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001173 * kmod_module_get_size:
1174 * @mod: kmod module
1175 *
1176 * Get the size of this kmod module as returned by Linux kernel. It reads the
1177 * file /proc/modules to search for this module and get its size.
1178 *
1179 * Returns: the size of this kmod module.
1180 */
1181KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1182{
1183 // FIXME TODO: this should be available from /sys/module/foo
1184 FILE *fp;
1185 char line[4096];
1186 int lineno = 0;
1187 long size = -ENOENT;
1188
1189 if (mod == NULL)
1190 return -ENOENT;
1191
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001192 fp = fopen("/proc/modules", "re");
Lucas De Marchi2d7bab52011-12-14 12:10:02 -02001193 if (fp == NULL) {
1194 int err = -errno;
1195 ERR(mod->ctx,
1196 "could not open /proc/modules: %s\n", strerror(errno));
1197 return err;
1198 }
1199
1200 while (fgets(line, sizeof(line), fp)) {
1201 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1202 long value;
1203
1204 lineno++;
1205 if (tok == NULL || !streq(tok, mod->name))
1206 continue;
1207
1208 tok = strtok_r(NULL, " \t", &saveptr);
1209 if (tok == NULL) {
1210 ERR(mod->ctx,
1211 "invalid line format at /proc/modules:%d\n", lineno);
1212 break;
1213 }
1214
1215 value = strtol(tok, &endptr, 10);
1216 if (endptr == tok || *endptr != '\0') {
1217 ERR(mod->ctx,
1218 "invalid line format at /proc/modules:%d\n", lineno);
1219 break;
1220 }
1221
1222 size = value;
1223 break;
1224 }
1225 fclose(fp);
1226 return size;
1227}
1228
1229/**
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001230 * kmod_module_get_refcnt:
1231 * @mod: kmod module
1232 *
1233 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1234 * /sys filesystem.
1235 *
1236 * Returns: 0 on success or < 0 on failure.
1237 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001238KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1239{
1240 char path[PATH_MAX];
1241 long refcnt;
1242 int fd, err;
1243
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001244 if (mod == NULL)
1245 return -ENOENT;
1246
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001247 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
Cristian Rodríguez79e5ea92011-12-16 02:49:54 -03001248 fd = open(path, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001249 if (fd < 0) {
1250 err = -errno;
1251 ERR(mod->ctx, "could not open '%s': %s\n",
1252 path, strerror(errno));
1253 return err;
1254 }
1255
1256 err = read_str_long(fd, &refcnt, 10);
1257 close(fd);
1258 if (err < 0) {
1259 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1260 path, strerror(-err));
1261 return err;
1262 }
1263
1264 return (int)refcnt;
1265}
1266
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001267/**
1268 * kmod_module_get_holders:
1269 * @mod: kmod module
1270 *
1271 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1272 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1273 *
1274 * Returns: a new list of kmod modules on success or NULL on failure.
1275 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001276KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1277{
1278 char dname[PATH_MAX];
1279 struct kmod_list *list = NULL;
1280 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001281
1282 if (mod == NULL)
1283 return NULL;
Lucas De Marchi818f8e82011-12-15 13:35:40 -02001284
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001285 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1286
1287 d = opendir(dname);
1288 if (d == NULL) {
1289 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001290 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001291 return NULL;
1292 }
1293
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001294 for (;;) {
1295 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001296 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001297 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001298 int err;
1299
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001300 err = readdir_r(d, &de, &entp);
1301 if (err != 0) {
1302 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1303 mod->name, strerror(-err));
1304 goto fail;
1305 }
1306
1307 if (entp == NULL)
1308 break;
1309
1310 if (de.d_name[0] == '.') {
1311 if (de.d_name[1] == '\0' ||
1312 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001313 continue;
1314 }
1315
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001316 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001317 if (err < 0) {
1318 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001319 de.d_name, strerror(-err));
1320 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001321 }
1322
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001323 l = kmod_list_append(list, holder);
1324 if (l != NULL) {
1325 list = l;
1326 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001327 ERR(mod->ctx, "out of memory\n");
1328 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001329 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001330 }
1331 }
1332
1333 closedir(d);
1334 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -02001335
1336fail:
1337 closedir(d);
1338 kmod_module_unref_list(list);
1339 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001340}
1341
1342struct kmod_module_section {
1343 unsigned long address;
1344 char name[];
1345};
1346
1347static void kmod_module_section_free(struct kmod_module_section *section)
1348{
1349 free(section);
1350}
1351
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001352/**
1353 * kmod_module_get_sections:
1354 * @mod: kmod module
1355 *
1356 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1357 * structure contained in this list is internal to libkmod and their fields
1358 * can be obtained by calling kmod_module_section_get_name() and
1359 * kmod_module_section_get_address().
1360 *
1361 * After use, free the @list by calling kmod_module_section_free_list().
1362 *
1363 * Returns: a new list of kmod module sections on success or NULL on failure.
1364 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001365KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1366{
1367 char dname[PATH_MAX];
1368 struct kmod_list *list = NULL;
1369 DIR *d;
1370 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001371
1372 if (mod == NULL)
1373 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001374
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001375 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1376
1377 d = opendir(dname);
1378 if (d == NULL) {
1379 ERR(mod->ctx, "could not open '%s': %s\n",
1380 dname, strerror(errno));
1381 return NULL;
1382 }
1383
1384 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001385
1386 for (;;) {
1387 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001388 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001389 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001390 unsigned long address;
1391 size_t namesz;
1392 int fd, err;
1393
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001394 err = readdir_r(d, &de, &entp);
1395 if (err != 0) {
1396 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1397 mod->name, strerror(-err));
1398 goto fail;
1399 }
1400
1401 if (de.d_name[0] == '.') {
1402 if (de.d_name[1] == '\0' ||
1403 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001404 continue;
1405 }
1406
Cristian Rodríguez8e3e5832011-12-16 14:46:52 -03001407 fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001408 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001409 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1410 dname, de.d_name);
1411 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001412 }
1413
1414 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001415 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001416 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001417 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1418 dname, de.d_name);
1419 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001420 }
1421
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001422 namesz = strlen(de.d_name) + 1;
1423 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001424
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001425 if (section == NULL) {
1426 ERR(mod->ctx, "out of memory\n");
1427 goto fail;
1428 }
1429
1430 section->address = address;
1431 memcpy(section->name, de.d_name, namesz);
1432
1433 l = kmod_list_append(list, section);
1434 if (l != NULL) {
1435 list = l;
1436 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001437 ERR(mod->ctx, "out of memory\n");
1438 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001439 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001440 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001441 }
1442
1443 closedir(d);
1444 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001445
1446fail:
1447 closedir(d);
1448 kmod_module_unref_list(list);
1449 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001450}
1451
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001452/**
1453 * kmod_module_section_get_module_name:
1454 * @entry: a list entry representing a kmod module section
1455 *
1456 * Get the name of a kmod module section.
1457 *
1458 * After use, free the @list by calling kmod_module_section_free_list().
1459 *
1460 * Returns: the name of this kmod module section on success or NULL on
1461 * failure. The string is owned by the section, do not free it.
1462 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001463KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1464{
1465 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001466
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001467 if (entry == NULL)
1468 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001469
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001470 section = entry->data;
1471 return section->name;
1472}
1473
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001474/**
1475 * kmod_module_section_get_address:
1476 * @entry: a list entry representing a kmod module section
1477 *
1478 * Get the address of a kmod module section.
1479 *
1480 * After use, free the @list by calling kmod_module_section_free_list().
1481 *
1482 * Returns: the address of this kmod module section on success or ULONG_MAX
1483 * on failure.
1484 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001485KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1486{
1487 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001488
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001489 if (entry == NULL)
1490 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001491
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001492 section = entry->data;
1493 return section->address;
1494}
1495
Lucas De Marchi7afc98a2011-12-14 12:06:18 -02001496/**
1497 * kmod_module_section_free_list:
1498 * @list: kmod module section list
1499 *
1500 * Release the resources taken by @list
1501 */
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001502KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1503{
1504 while (list) {
1505 kmod_module_section_free(list->data);
1506 list = kmod_list_remove(list);
1507 }
1508}