blob: 3e99c0ccdec6d14cc442a7ae62fd6ea8b913c877 [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
8 * License as published by the Free Software Foundation version 2.1.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Lucas De Marchi7636e722011-12-01 17:56:03 -020020#include <assert.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020021#include <stdio.h>
22#include <stdlib.h>
23#include <stddef.h>
24#include <stdarg.h>
25#include <unistd.h>
26#include <errno.h>
27#include <string.h>
28#include <ctype.h>
29#include <inttypes.h>
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -020030#include <limits.h>
31#include <dirent.h>
Lucas De Marchi8f788d52011-11-25 01:22:56 -020032#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/mman.h>
35#include <string.h>
36
37#include "libkmod.h"
38#include "libkmod-private.h"
39
40/**
41 * kmod_module:
42 *
43 * Opaque object representing a module.
44 */
45struct kmod_module {
46 struct kmod_ctx *ctx;
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -020047 char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020048 struct kmod_list *dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020049 char *options;
50 char *install_commands;
51 char *remove_commands;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -020052 int n_dep;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020053 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020054 struct {
55 bool dep : 1;
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -020056 bool options : 1;
57 bool install_commands : 1;
58 bool remove_commands : 1;
Lucas De Marchi7636e722011-12-01 17:56:03 -020059 } init;
Lucas De Marchid753b8c2011-12-05 18:14:51 -020060 char name[];
Lucas De Marchi8f788d52011-11-25 01:22:56 -020061};
62
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -020063inline char *modname_normalize(const char *modname, char buf[NAME_MAX],
Lucas De Marchi6c343b12011-12-06 09:01:01 -020064 size_t *len)
Lucas De Marchi8f788d52011-11-25 01:22:56 -020065{
Lucas De Marchid753b8c2011-12-05 18:14:51 -020066 size_t s;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020067
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020068 for (s = 0; s < NAME_MAX - 1; s++) {
69 const char c = modname[s];
70 if (c == '-')
71 buf[s] = '_';
72 else if (c == '\0' || c == '.')
73 break;
74 else
75 buf[s] = c;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020076 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -020077
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020078 buf[s] = '\0';
Lucas De Marchid753b8c2011-12-05 18:14:51 -020079
80 if (len)
81 *len = s;
82
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -020083 return buf;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020084}
85
Lucas De Marchi6c343b12011-12-06 09:01:01 -020086static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len)
87{
88 char *modname;
89
90 modname = basename(path);
91 if (modname == NULL || modname[0] == '\0')
92 return NULL;
93
94 return modname_normalize(modname, buf, len);
95}
96
Lucas De Marchic35347f2011-12-12 10:48:02 -020097static inline const char *path_join(const char *path, size_t prefixlen,
98 char buf[PATH_MAX])
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -020099{
100 size_t pathlen;
101
102 if (path[0] == '/')
103 return path;
104
105 pathlen = strlen(path);
106 if (prefixlen + pathlen + 1 >= PATH_MAX)
107 return NULL;
108
109 memcpy(buf + prefixlen, path, pathlen + 1);
110 return buf;
111}
112
Lucas De Marchi671d4892011-12-05 20:23:05 -0200113int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -0200114{
115 struct kmod_ctx *ctx = mod->ctx;
116 struct kmod_list *list = NULL;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200117 const char *dirname;
118 char buf[PATH_MAX];
Lucas De Marchi7636e722011-12-01 17:56:03 -0200119 char *p, *saveptr;
Lucas De Marchi45f27782011-12-12 17:23:04 -0200120 int err = 0, n = 0;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200121 size_t dirnamelen;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200122
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200123 if (mod->init.dep)
124 return mod->n_dep;
125 assert(mod->dep == NULL);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200126 mod->init.dep = true;
127
128 p = strchr(line, ':');
129 if (p == NULL)
130 return 0;
131
Lucas De Marchi671d4892011-12-05 20:23:05 -0200132 *p = '\0';
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200133 dirname = kmod_get_dirname(mod->ctx);
134 dirnamelen = strlen(dirname);
135 if (dirnamelen + 2 >= PATH_MAX)
136 return 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200137
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200138 memcpy(buf, dirname, dirnamelen);
139 buf[dirnamelen] = '/';
140 dirnamelen++;
141 buf[dirnamelen] = '\0';
142
143 if (mod->path == NULL) {
144 const char *str = path_join(line, dirnamelen, buf);
145 if (str == NULL)
146 return 0;
147 mod->path = strdup(str);
148 if (mod->path == NULL)
149 return 0;
150 }
Lucas De Marchi671d4892011-12-05 20:23:05 -0200151
Lucas De Marchi7636e722011-12-01 17:56:03 -0200152 p++;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200153 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
154 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200155 struct kmod_module *depmod;
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200156 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200157
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200158 path = path_join(p, dirnamelen, buf);
159 if (path == NULL) {
160 ERR(ctx, "could not join path '%s' and '%s'.\n",
161 dirname, p);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200162 goto fail;
163 }
164
Gustavo Sverzut Barbierie18ad352011-12-08 04:44:03 -0200165 err = kmod_module_new_from_path(ctx, path, &depmod);
166 if (err < 0) {
167 ERR(ctx, "ctx=%p path=%s error=%s\n",
168 ctx, path, strerror(-err));
169 goto fail;
170 }
171
172 DBG(ctx, "add dep: %s\n", path);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200173
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200174 list = kmod_list_append(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200175 n++;
176 }
177
178 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
179
180 mod->dep = list;
Gustavo Sverzut Barbierib6a534f2011-12-10 20:36:22 -0200181 mod->n_dep = n;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200182 return n;
183
184fail:
185 kmod_module_unref_list(list);
186 mod->init.dep = false;
187 return err;
188}
189
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200190KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
191 const char *name,
192 struct kmod_module **mod)
193{
194 struct kmod_module *m;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200195 size_t namelen;
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200196 char name_norm[NAME_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200197
198 if (ctx == NULL || name == NULL)
199 return -ENOENT;
200
Gustavo Sverzut Barbierie1a6b302011-12-08 04:10:49 -0200201 modname_normalize(name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200202
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200203 m = kmod_pool_get_module(ctx, name_norm);
204 if (m != NULL) {
205 *mod = kmod_module_ref(m);
206 return 0;
207 }
208
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200209 m = calloc(1, sizeof(*m) + namelen + 1);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200210 if (m == NULL) {
211 free(m);
212 return -ENOMEM;
213 }
214
215 m->ctx = kmod_ref(ctx);
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200216 memcpy(m->name, name_norm, namelen + 1);
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200217 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200218
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200219 kmod_pool_add_module(ctx, m);
220
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200221 *mod = m;
222
223 return 0;
224}
225
226KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
227 const char *path,
228 struct kmod_module **mod)
229{
230 struct kmod_module *m;
231 int err;
232 struct stat st;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200233 char name[NAME_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200234 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200235 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200236
237 if (ctx == NULL || path == NULL)
238 return -ENOENT;
239
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200240 abspath = path_make_absolute_cwd(path);
241 if (abspath == NULL)
242 return -ENOMEM;
243
244 err = stat(abspath, &st);
245 if (err < 0) {
246 free(abspath);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200247 return -errno;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200248 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200249
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200250 path_to_modname(path, name, &namelen);
251
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200252 m = kmod_pool_get_module(ctx, name);
253 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200254 if (m->path == NULL)
255 m->path = abspath;
256 else if (streq(m->path, abspath))
257 free(abspath);
258 else {
259 ERR(ctx, "kmod_module '%s' already exists with different path\n",
260 name);
261 free(abspath);
262 return -EEXIST;
263 }
264
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200265 *mod = kmod_module_ref(m);
266 return 0;
267 }
268
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200269 m = calloc(1, sizeof(*m) + namelen + 1);
270 if (m == NULL)
271 return -errno;
272
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200273 m->ctx = kmod_ref(ctx);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200274 memcpy(m->name, name, namelen);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200275 m->path = abspath;
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200276 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200277
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200278 kmod_pool_add_module(ctx, m);
279
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200280 *mod = m;
281
282 return 0;
283}
284
285KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
286{
287 if (mod == NULL)
288 return NULL;
289
290 if (--mod->refcount > 0)
291 return mod;
292
293 DBG(mod->ctx, "kmod_module %p released\n", mod);
294
Lucas De Marchi7636e722011-12-01 17:56:03 -0200295 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200296 kmod_unref(mod->ctx);
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200297 free(mod->options);
298 free(mod->install_commands);
299 free(mod->remove_commands);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200300 free(mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200301 free(mod);
302 return NULL;
303}
304
305KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
306{
307 if (mod == NULL)
308 return NULL;
309
310 mod->refcount++;
311
312 return mod;
313}
314
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200315#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
316 do { \
317 if ((_err) < 0) \
318 goto _label_err; \
319 if (*(_list) != NULL) \
320 goto finish; \
321 } while (0)
322
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200323KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200324 const char *given_alias,
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200325 struct kmod_list **list)
326{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200327 int err;
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200328 char alias[NAME_MAX];
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200329
330 if (ctx == NULL || alias == NULL)
331 return -ENOENT;
332
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200333 if (list == NULL || *list != NULL) {
334 ERR(ctx, "An empty list is needed to create lookup\n");
335 return -ENOSYS;
336 }
337
Gustavo Sverzut Barbierid917f272011-12-10 21:00:19 -0200338 modname_normalize(given_alias, alias, NULL);
339
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200340 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200341 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200342 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200343
Lucas De Marchi64700e42011-12-01 15:57:53 -0200344 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
345 CHECK_ERR_AND_FINISH(err, fail, list, finish);
346
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200347 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
348 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200349
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200350 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
351 CHECK_ERR_AND_FINISH(err, fail, list, finish);
352
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200353finish:
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200354
355 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200356fail:
357 kmod_module_unref_list(*list);
358 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200359 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200360}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200361#undef CHECK_ERR_AND_FINISH
362
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200363
364KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
365{
366 for (; list != NULL; list = kmod_list_remove(list))
367 kmod_module_unref(list->data);
368
369 return 0;
370}
371
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200372KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200373{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200374 struct kmod_list *l, *l_new, *list_new = NULL;
375
376 if (mod == NULL)
377 return NULL;
378
Lucas De Marchi671d4892011-12-05 20:23:05 -0200379 if (!mod->init.dep) {
380 /* lazy init */
381 char *line = kmod_search_moddep(mod->ctx, mod->name);
382
383 if (line == NULL)
384 return NULL;
385
386 kmod_module_parse_depline((struct kmod_module *)mod, line);
387 free(line);
388
389 if (!mod->init.dep)
390 return NULL;
391 }
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200392
393 kmod_list_foreach(l, mod->dep) {
394 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
395 if (l_new == NULL) {
396 kmod_module_unref(l->data);
397 goto fail;
398 }
399
400 list_new = l_new;
401 }
402
403 return list_new;
404
405fail:
406 ERR(mod->ctx, "out of memory\n");
407 kmod_module_unref_list(list_new);
408 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200409}
410
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200411KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200412{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200413 if (entry == NULL)
414 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200415
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200416 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200417}
418
Gustavo Sverzut Barbieri69f9dd42011-12-04 14:02:30 -0200419KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
420{
421 // FIXME TODO: this should be available from /sys/module/foo
422 FILE *fp;
423 char line[4096];
424 int lineno = 0;
425 long size = -ENOENT;
426
427 if (mod == NULL)
428 return -ENOENT;
429
430 fp = fopen("/proc/modules", "r");
431 if (fp == NULL) {
432 int err = -errno;
433 ERR(mod->ctx,
434 "could not open /proc/modules: %s\n", strerror(errno));
435 return err;
436 }
437
438 while (fgets(line, sizeof(line), fp)) {
439 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
440 long value;
441
442 lineno++;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200443 if (tok == NULL || !streq(tok, mod->name))
Gustavo Sverzut Barbieri69f9dd42011-12-04 14:02:30 -0200444 continue;
445
446 tok = strtok_r(NULL, " \t", &saveptr);
447 if (tok == NULL) {
448 ERR(mod->ctx,
449 "invalid line format at /proc/modules:%d\n", lineno);
450 break;
451 }
452
453 value = strtol(tok, &endptr, 10);
454 if (endptr == tok || *endptr != '\0') {
455 ERR(mod->ctx,
456 "invalid line format at /proc/modules:%d\n", lineno);
457 break;
458 }
459
460 size = value;
461 break;
462 }
463 fclose(fp);
464 return size;
465}
466
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200467KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200468{
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200469 return mod->name;
470}
471
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200472KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200473{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200474 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200475
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200476 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200477
Lucas De Marchie005fac2011-12-08 10:42:34 -0200478 if (mod->path != NULL)
479 return mod->path;
480 if (mod->init.dep)
481 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200482
Lucas De Marchie005fac2011-12-08 10:42:34 -0200483 /* lazy init */
484 line = kmod_search_moddep(mod->ctx, mod->name);
485 if (line == NULL)
486 return NULL;
487
488 kmod_module_parse_depline((struct kmod_module *) mod, line);
489 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200490
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200491 return mod->path;
492}
493
494
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200495extern long delete_module(const char *name, unsigned int flags);
496
497KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
498 unsigned int flags)
499{
500 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200501
502 if (mod == NULL)
503 return -ENOENT;
504
505 /* Filter out other flags */
506 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
507
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200508 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200509 if (err != 0) {
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200510 ERR(mod->ctx, "Removing '%s': %s\n", mod->name,
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200511 strerror(-err));
512 return err;
513 }
514
515 return 0;
516}
517
518extern long init_module(void *mem, unsigned long len, const char *args);
519
520KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200521 unsigned int flags,
522 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200523{
524 int err;
525 void *mmaped_file;
526 struct stat st;
527 int fd;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200528 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200529
530 if (mod == NULL)
531 return -ENOENT;
532
533 if (mod->path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200534 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200535 return -ENOSYS;
536 }
537
538 if (flags != 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200539 INFO(mod->ctx, "Flags are not implemented yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200540
541 if ((fd = open(mod->path, O_RDONLY)) < 0) {
542 err = -errno;
543 return err;
544 }
545
Lucas De Marchib418a822011-12-01 23:13:27 -0200546 fstat(fd, &st);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200547
548 if ((mmaped_file = mmap(0, st.st_size, PROT_READ,
549 MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
550 close(fd);
551 return -errno;
552 }
553
554 err = init_module(mmaped_file, st.st_size, args);
555 if (err < 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200556 ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200557
558 munmap(mmaped_file, st.st_size);
559 close(fd);
560
561 return err;
562}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200563
Lucas De Marchi49ce6d02011-12-12 13:56:47 -0200564KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
565{
566 if (mod == NULL)
567 return NULL;
568
569 if (!mod->init.options) {
570 /* lazy init */
571 struct kmod_module *m = (struct kmod_module *)mod;
572 const struct kmod_list *l, *ctx_options;
573 char *opts = NULL;
574 size_t optslen = 0;
575
576 ctx_options = kmod_get_options(mod->ctx);
577
578 kmod_list_foreach(l, ctx_options) {
579 const char *modname = kmod_option_get_modname(l);
580 const char *str;
581 size_t len;
582 void *tmp;
583
584 if (strcmp(modname, mod->name) != 0)
585 continue;
586
587 str = kmod_option_get_options(l);
588 len = strlen(str);
589 if (len < 1)
590 continue;
591
592 tmp = realloc(opts, optslen + len + 2);
593 if (tmp == NULL) {
594 free(opts);
595 goto failed;
596 }
597
598 opts = tmp;
599
600 if (optslen > 0) {
601 opts[optslen] = ' ';
602 optslen++;
603 }
604
605 memcpy(opts + optslen, str, len);
606 optslen += len;
607 opts[optslen] = '\0';
608 }
609
610 m->init.options = true;
611 m->options = opts;
612 }
613
614 return mod->options;
615
616failed:
617 ERR(mod->ctx, "out of memory\n");
618 return NULL;
619}
620
621KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
622{
623 if (mod == NULL)
624 return NULL;
625
626 if (!mod->init.install_commands) {
627 /* lazy init */
628 struct kmod_module *m = (struct kmod_module *)mod;
629 const struct kmod_list *l, *ctx_install_commands;
630 char *cmds = NULL;
631 size_t cmdslen = 0;
632
633 ctx_install_commands = kmod_get_install_commands(mod->ctx);
634
635 kmod_list_foreach(l, ctx_install_commands) {
636 const char *modname = kmod_command_get_modname(l);
637 const char *str;
638 size_t len;
639 void *tmp;
640
641 if (strcmp(modname, mod->name) != 0)
642 continue;
643
644 str = kmod_command_get_command(l);
645 len = strlen(str);
646 if (len < 1)
647 continue;
648
649 tmp = realloc(cmds, cmdslen + len + 2);
650 if (tmp == NULL) {
651 free(cmds);
652 goto failed;
653 }
654
655 cmds = tmp;
656
657 if (cmdslen > 0) {
658 cmds[cmdslen] = ';';
659 cmdslen++;
660 }
661
662 memcpy(cmds + cmdslen, str, len);
663 cmdslen += len;
664 cmds[cmdslen] = '\0';
665 }
666
667 m->init.install_commands = true;
668 m->install_commands = cmds;
669 }
670
671 return mod->install_commands;
672
673failed:
674 ERR(mod->ctx, "out of memory\n");
675 return NULL;
676}
677
678KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
679{
680 if (mod == NULL)
681 return NULL;
682
683 if (!mod->init.remove_commands) {
684 /* lazy init */
685 struct kmod_module *m = (struct kmod_module *)mod;
686 const struct kmod_list *l, *ctx_remove_commands;
687 char *cmds = NULL;
688 size_t cmdslen = 0;
689
690 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
691
692 kmod_list_foreach(l, ctx_remove_commands) {
693 const char *modname = kmod_command_get_modname(l);
694 const char *str;
695 size_t len;
696 void *tmp;
697
698 if (strcmp(modname, mod->name) != 0)
699 continue;
700
701 str = kmod_command_get_command(l);
702 len = strlen(str);
703 if (len < 1)
704 continue;
705
706 tmp = realloc(cmds, cmdslen + len + 2);
707 if (tmp == NULL) {
708 free(cmds);
709 goto failed;
710 }
711
712 cmds = tmp;
713
714 if (cmdslen > 0) {
715 cmds[cmdslen] = ';';
716 cmdslen++;
717 }
718
719 memcpy(cmds + cmdslen, str, len);
720 cmdslen += len;
721 cmds[cmdslen] = '\0';
722 }
723
724 m->init.remove_commands = true;
725 m->remove_commands = cmds;
726 }
727
728 return mod->remove_commands;
729
730failed:
731 ERR(mod->ctx, "out of memory\n");
732 return NULL;
733}
734
735/**
736 * SECTION:libkmod-loaded
737 * @short_description: currently loaded modules
738 *
739 * Information about currently loaded modules, as reported by Linux kernel.
740 * These information are not cached by libkmod and are always read from /sys
741 * and /proc/modules.
742 */
743
744/**
745 * kmod_module_new_from_loaded:
746 * @ctx: kmod library context
747 * @list: where to save the list of loaded modules
748 *
749 * Get a list of all modules currently loaded in kernel. It uses /proc/modules
750 * to get the names of loaded modules and to create kmod_module objects by
751 * calling kmod_module_new_from_name() in each of them. They are put are put
752 * in @list in no particular order.
753 *
754 * All the returned modules get their refcount incremented (or are created if
755 * they do not exist yet). After using the list, release the resources by
756 * calling kmod_module_unref_list().
757 *
758 * Returns: 0 on success or < 0 on error.
759 */
760KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
761 struct kmod_list **list)
762{
763 struct kmod_list *l = NULL;
764 FILE *fp;
765 char line[4096];
766
767 if (ctx == NULL || list == NULL)
768 return -ENOENT;
769
770 fp = fopen("/proc/modules", "r");
771 if (fp == NULL) {
772 int err = -errno;
773 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
774 return err;
775 }
776
777 while (fgets(line, sizeof(line), fp)) {
778 struct kmod_module *m;
779 struct kmod_list *node;
780 int err;
781 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
782
783 err = kmod_module_new_from_name(ctx, name, &m);
784 if (err < 0) {
785 ERR(ctx, "could not get module from name '%s': %s\n",
786 name, strerror(-err));
787 continue;
788 }
789
790 node = kmod_list_append(l, m);
791 if (node)
792 l = node;
793 else {
794 ERR(ctx, "out of memory\n");
795 kmod_module_unref(m);
796 }
797 }
798
799 fclose(fp);
800 *list = l;
801
802 return 0;
803}
804
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200805KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
806{
807 switch (state) {
808 case KMOD_MODULE_BUILTIN:
809 return "builtin";
810 case KMOD_MODULE_LIVE:
811 return "live";
812 case KMOD_MODULE_COMING:
813 return "coming";
814 case KMOD_MODULE_GOING:
815 return "going";
816 default:
817 return NULL;
818 }
819}
820
821KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
822{
823 char path[PATH_MAX], buf[32];
824 int fd, err, pathlen;
825
826 pathlen = snprintf(path, sizeof(path),
827 "/sys/module/%s/initstate", mod->name);
828 fd = open(path, O_RDONLY);
829 if (fd < 0) {
830 err = -errno;
831
832 if (pathlen > (int)sizeof("/initstate") - 1) {
833 struct stat st;
834 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
835 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
836 return KMOD_MODULE_BUILTIN;
837 }
838
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -0200839 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200840 path, strerror(-err));
841 return err;
842 }
843
844 err = read_str_safe(fd, buf, sizeof(buf));
845 close(fd);
846 if (err < 0) {
847 ERR(mod->ctx, "could not read from '%s': %s\n",
848 path, strerror(-err));
849 return err;
850 }
851
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200852 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200853 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200854 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200855 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200856 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200857 return KMOD_MODULE_GOING;
858
859 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
860 return -EINVAL;
861}
862
863KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
864{
865 char path[PATH_MAX];
866 long refcnt;
867 int fd, err;
868
869 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
870 fd = open(path, O_RDONLY);
871 if (fd < 0) {
872 err = -errno;
873 ERR(mod->ctx, "could not open '%s': %s\n",
874 path, strerror(errno));
875 return err;
876 }
877
878 err = read_str_long(fd, &refcnt, 10);
879 close(fd);
880 if (err < 0) {
881 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
882 path, strerror(-err));
883 return err;
884 }
885
886 return (int)refcnt;
887}
888
889KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
890{
891 char dname[PATH_MAX];
892 struct kmod_list *list = NULL;
893 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200894
895 if (mod == NULL)
896 return NULL;
897 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
898
899 d = opendir(dname);
900 if (d == NULL) {
901 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200902 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200903 return NULL;
904 }
905
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200906 for (;;) {
907 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200908 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200909 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200910 int err;
911
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200912 err = readdir_r(d, &de, &entp);
913 if (err != 0) {
914 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
915 mod->name, strerror(-err));
916 goto fail;
917 }
918
919 if (entp == NULL)
920 break;
921
922 if (de.d_name[0] == '.') {
923 if (de.d_name[1] == '\0' ||
924 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200925 continue;
926 }
927
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200928 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200929 if (err < 0) {
930 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200931 de.d_name, strerror(-err));
932 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200933 }
934
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200935 l = kmod_list_append(list, holder);
936 if (l != NULL) {
937 list = l;
938 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200939 ERR(mod->ctx, "out of memory\n");
940 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200941 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200942 }
943 }
944
945 closedir(d);
946 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200947
948fail:
949 closedir(d);
950 kmod_module_unref_list(list);
951 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200952}
953
954struct kmod_module_section {
955 unsigned long address;
956 char name[];
957};
958
959static void kmod_module_section_free(struct kmod_module_section *section)
960{
961 free(section);
962}
963
964KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
965{
966 char dname[PATH_MAX];
967 struct kmod_list *list = NULL;
968 DIR *d;
969 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200970
971 if (mod == NULL)
972 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200973
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200974 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
975
976 d = opendir(dname);
977 if (d == NULL) {
978 ERR(mod->ctx, "could not open '%s': %s\n",
979 dname, strerror(errno));
980 return NULL;
981 }
982
983 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200984
985 for (;;) {
986 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200987 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200988 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200989 unsigned long address;
990 size_t namesz;
991 int fd, err;
992
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200993 err = readdir_r(d, &de, &entp);
994 if (err != 0) {
995 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
996 mod->name, strerror(-err));
997 goto fail;
998 }
999
1000 if (de.d_name[0] == '.') {
1001 if (de.d_name[1] == '\0' ||
1002 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001003 continue;
1004 }
1005
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001006 fd = openat(dfd, de.d_name, O_RDONLY);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001007 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001008 ERR(mod->ctx, "could not open '%s/%s': %m\n",
1009 dname, de.d_name);
1010 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001011 }
1012
1013 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001014 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001015 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001016 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1017 dname, de.d_name);
1018 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001019 }
1020
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001021 namesz = strlen(de.d_name) + 1;
1022 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001023
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001024 if (section == NULL) {
1025 ERR(mod->ctx, "out of memory\n");
1026 goto fail;
1027 }
1028
1029 section->address = address;
1030 memcpy(section->name, de.d_name, namesz);
1031
1032 l = kmod_list_append(list, section);
1033 if (l != NULL) {
1034 list = l;
1035 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001036 ERR(mod->ctx, "out of memory\n");
1037 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001038 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001039 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001040 }
1041
1042 closedir(d);
1043 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -02001044
1045fail:
1046 closedir(d);
1047 kmod_module_unref_list(list);
1048 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001049}
1050
1051KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1052{
1053 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001054
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001055 if (entry == NULL)
1056 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001057
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001058 section = entry->data;
1059 return section->name;
1060}
1061
1062KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1063{
1064 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001065
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001066 if (entry == NULL)
1067 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001068
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -02001069 section = entry->data;
1070 return section->address;
1071}
1072
1073KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1074{
1075 while (list) {
1076 kmod_module_section_free(list->data);
1077 list = kmod_list_remove(list);
1078 }
1079}