blob: 500193d022181c66d9b950ab8f32aa9d87437e19 [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;
120 int err, 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
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200472/*
473 * Relative paths are relative to dirname. Absolute paths are only used when
474 * user created kmod_module by giving a path
475 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200476KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200477{
Lucas De Marchie005fac2011-12-08 10:42:34 -0200478 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200479
Gustavo Sverzut Barbierid01c67e2011-12-11 19:42:02 -0200480 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200481
Lucas De Marchie005fac2011-12-08 10:42:34 -0200482 if (mod->path != NULL)
483 return mod->path;
484 if (mod->init.dep)
485 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200486
Lucas De Marchie005fac2011-12-08 10:42:34 -0200487 /* lazy init */
488 line = kmod_search_moddep(mod->ctx, mod->name);
489 if (line == NULL)
490 return NULL;
491
492 kmod_module_parse_depline((struct kmod_module *) mod, line);
493 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200494
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200495 return mod->path;
496}
497
498
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200499extern long delete_module(const char *name, unsigned int flags);
500
501KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
502 unsigned int flags)
503{
504 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200505
506 if (mod == NULL)
507 return -ENOENT;
508
509 /* Filter out other flags */
510 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
511
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200512 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200513 if (err != 0) {
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200514 ERR(mod->ctx, "Removing '%s': %s\n", mod->name,
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200515 strerror(-err));
516 return err;
517 }
518
519 return 0;
520}
521
522extern long init_module(void *mem, unsigned long len, const char *args);
523
524KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200525 unsigned int flags,
526 const char *options)
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200527{
528 int err;
529 void *mmaped_file;
530 struct stat st;
531 int fd;
Gustavo Sverzut Barbieri3a721bb2011-12-10 21:02:39 -0200532 const char *args = options ? options : "";
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200533
534 if (mod == NULL)
535 return -ENOENT;
536
537 if (mod->path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200538 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200539 return -ENOSYS;
540 }
541
542 if (flags != 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200543 INFO(mod->ctx, "Flags are not implemented yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200544
545 if ((fd = open(mod->path, O_RDONLY)) < 0) {
546 err = -errno;
547 return err;
548 }
549
Lucas De Marchib418a822011-12-01 23:13:27 -0200550 fstat(fd, &st);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200551
552 if ((mmaped_file = mmap(0, st.st_size, PROT_READ,
553 MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
554 close(fd);
555 return -errno;
556 }
557
558 err = init_module(mmaped_file, st.st_size, args);
559 if (err < 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200560 ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200561
562 munmap(mmaped_file, st.st_size);
563 close(fd);
564
565 return err;
566}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200567
568KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
569{
570 switch (state) {
571 case KMOD_MODULE_BUILTIN:
572 return "builtin";
573 case KMOD_MODULE_LIVE:
574 return "live";
575 case KMOD_MODULE_COMING:
576 return "coming";
577 case KMOD_MODULE_GOING:
578 return "going";
579 default:
580 return NULL;
581 }
582}
583
584KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
585{
586 char path[PATH_MAX], buf[32];
587 int fd, err, pathlen;
588
589 pathlen = snprintf(path, sizeof(path),
590 "/sys/module/%s/initstate", mod->name);
591 fd = open(path, O_RDONLY);
592 if (fd < 0) {
593 err = -errno;
594
595 if (pathlen > (int)sizeof("/initstate") - 1) {
596 struct stat st;
597 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
598 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
599 return KMOD_MODULE_BUILTIN;
600 }
601
Gustavo Sverzut Barbieri926f67a2011-12-11 19:33:03 -0200602 DBG(mod->ctx, "could not open '%s': %s\n",
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200603 path, strerror(-err));
604 return err;
605 }
606
607 err = read_str_safe(fd, buf, sizeof(buf));
608 close(fd);
609 if (err < 0) {
610 ERR(mod->ctx, "could not read from '%s': %s\n",
611 path, strerror(-err));
612 return err;
613 }
614
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200615 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200616 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200617 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200618 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200619 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200620 return KMOD_MODULE_GOING;
621
622 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
623 return -EINVAL;
624}
625
626KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
627{
628 char path[PATH_MAX];
629 long refcnt;
630 int fd, err;
631
632 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
633 fd = open(path, O_RDONLY);
634 if (fd < 0) {
635 err = -errno;
636 ERR(mod->ctx, "could not open '%s': %s\n",
637 path, strerror(errno));
638 return err;
639 }
640
641 err = read_str_long(fd, &refcnt, 10);
642 close(fd);
643 if (err < 0) {
644 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
645 path, strerror(-err));
646 return err;
647 }
648
649 return (int)refcnt;
650}
651
652KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
653{
654 char dname[PATH_MAX];
655 struct kmod_list *list = NULL;
656 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200657
658 if (mod == NULL)
659 return NULL;
660 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
661
662 d = opendir(dname);
663 if (d == NULL) {
664 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200665 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200666 return NULL;
667 }
668
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200669 for (;;) {
670 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200671 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200672 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200673 int err;
674
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200675 err = readdir_r(d, &de, &entp);
676 if (err != 0) {
677 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
678 mod->name, strerror(-err));
679 goto fail;
680 }
681
682 if (entp == NULL)
683 break;
684
685 if (de.d_name[0] == '.') {
686 if (de.d_name[1] == '\0' ||
687 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200688 continue;
689 }
690
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200691 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200692 if (err < 0) {
693 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200694 de.d_name, strerror(-err));
695 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200696 }
697
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200698 l = kmod_list_append(list, holder);
699 if (l != NULL) {
700 list = l;
701 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200702 ERR(mod->ctx, "out of memory\n");
703 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200704 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200705 }
706 }
707
708 closedir(d);
709 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200710
711fail:
712 closedir(d);
713 kmod_module_unref_list(list);
714 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200715}
716
717struct kmod_module_section {
718 unsigned long address;
719 char name[];
720};
721
722static void kmod_module_section_free(struct kmod_module_section *section)
723{
724 free(section);
725}
726
727KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
728{
729 char dname[PATH_MAX];
730 struct kmod_list *list = NULL;
731 DIR *d;
732 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200733
734 if (mod == NULL)
735 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200736
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200737 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
738
739 d = opendir(dname);
740 if (d == NULL) {
741 ERR(mod->ctx, "could not open '%s': %s\n",
742 dname, strerror(errno));
743 return NULL;
744 }
745
746 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200747
748 for (;;) {
749 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200750 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200751 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200752 unsigned long address;
753 size_t namesz;
754 int fd, err;
755
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200756 err = readdir_r(d, &de, &entp);
757 if (err != 0) {
758 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
759 mod->name, strerror(-err));
760 goto fail;
761 }
762
763 if (de.d_name[0] == '.') {
764 if (de.d_name[1] == '\0' ||
765 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200766 continue;
767 }
768
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200769 fd = openat(dfd, de.d_name, O_RDONLY);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200770 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200771 ERR(mod->ctx, "could not open '%s/%s': %m\n",
772 dname, de.d_name);
773 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200774 }
775
776 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200777 close(fd);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200778 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200779 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
780 dname, de.d_name);
781 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200782 }
783
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200784 namesz = strlen(de.d_name) + 1;
785 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200786
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200787 if (section == NULL) {
788 ERR(mod->ctx, "out of memory\n");
789 goto fail;
790 }
791
792 section->address = address;
793 memcpy(section->name, de.d_name, namesz);
794
795 l = kmod_list_append(list, section);
796 if (l != NULL) {
797 list = l;
798 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200799 ERR(mod->ctx, "out of memory\n");
800 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200801 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200802 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200803 }
804
805 closedir(d);
806 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200807
808fail:
809 closedir(d);
810 kmod_module_unref_list(list);
811 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200812}
813
814KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
815{
816 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200817
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200818 if (entry == NULL)
819 return NULL;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200820
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200821 section = entry->data;
822 return section->name;
823}
824
825KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
826{
827 struct kmod_module_section *section;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200828
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200829 if (entry == NULL)
830 return (unsigned long)-1;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200831
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200832 section = entry->data;
833 return section->address;
834}
835
836KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
837{
838 while (list) {
839 kmod_module_section_free(list->data);
840 list = kmod_list_remove(list);
841 }
842}
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200843
844KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
845{
846 if (mod == NULL)
847 return NULL;
848
849 if (!mod->init.options) {
850 /* lazy init */
851 struct kmod_module *m = (struct kmod_module *)mod;
852 const struct kmod_list *l, *ctx_options;
853 char *opts = NULL;
854 size_t optslen = 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200855
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200856 ctx_options = kmod_get_options(mod->ctx);
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200857
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200858 kmod_list_foreach(l, ctx_options) {
859 const char *modname = kmod_option_get_modname(l);
860 const char *str;
861 size_t len;
862 void *tmp;
863
864 if (strcmp(modname, mod->name) != 0)
865 continue;
866
867 str = kmod_option_get_options(l);
868 len = strlen(str);
869 if (len < 1)
870 continue;
871
872 tmp = realloc(opts, optslen + len + 2);
873 if (tmp == NULL) {
874 free(opts);
875 goto failed;
876 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200877
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200878 opts = tmp;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200879
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200880 if (optslen > 0) {
881 opts[optslen] = ' ';
882 optslen++;
883 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200884
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200885 memcpy(opts + optslen, str, len);
886 optslen += len;
887 opts[optslen] = '\0';
888 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200889
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200890 m->init.options = true;
891 m->options = opts;
892 }
893
894 return mod->options;
895
896failed:
897 ERR(mod->ctx, "out of memory\n");
898 return NULL;
899}
900
901KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
902{
903 if (mod == NULL)
904 return NULL;
905
906 if (!mod->init.install_commands) {
907 /* lazy init */
908 struct kmod_module *m = (struct kmod_module *)mod;
909 const struct kmod_list *l, *ctx_install_commands;
910 char *cmds = NULL;
911 size_t cmdslen = 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200912
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200913 ctx_install_commands = kmod_get_install_commands(mod->ctx);
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200914
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200915 kmod_list_foreach(l, ctx_install_commands) {
916 const char *modname = kmod_command_get_modname(l);
917 const char *str;
918 size_t len;
919 void *tmp;
920
921 if (strcmp(modname, mod->name) != 0)
922 continue;
923
924 str = kmod_command_get_command(l);
925 len = strlen(str);
926 if (len < 1)
927 continue;
928
929 tmp = realloc(cmds, cmdslen + len + 2);
930 if (tmp == NULL) {
931 free(cmds);
932 goto failed;
933 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200934
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200935 cmds = tmp;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200936
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200937 if (cmdslen > 0) {
938 cmds[cmdslen] = ';';
939 cmdslen++;
940 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200941
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200942 memcpy(cmds + cmdslen, str, len);
943 cmdslen += len;
944 cmds[cmdslen] = '\0';
945 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200946
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200947 m->init.install_commands = true;
948 m->install_commands = cmds;
949 }
950
951 return mod->install_commands;
952
953failed:
954 ERR(mod->ctx, "out of memory\n");
955 return NULL;
956}
957
958KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
959{
960 if (mod == NULL)
961 return NULL;
962
963 if (!mod->init.remove_commands) {
964 /* lazy init */
965 struct kmod_module *m = (struct kmod_module *)mod;
966 const struct kmod_list *l, *ctx_remove_commands;
967 char *cmds = NULL;
968 size_t cmdslen = 0;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200969
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200970 ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200971
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200972 kmod_list_foreach(l, ctx_remove_commands) {
973 const char *modname = kmod_command_get_modname(l);
974 const char *str;
975 size_t len;
976 void *tmp;
977
978 if (strcmp(modname, mod->name) != 0)
979 continue;
980
981 str = kmod_command_get_command(l);
982 len = strlen(str);
983 if (len < 1)
984 continue;
985
986 tmp = realloc(cmds, cmdslen + len + 2);
987 if (tmp == NULL) {
988 free(cmds);
989 goto failed;
990 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200991
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200992 cmds = tmp;
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200993
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200994 if (cmdslen > 0) {
995 cmds[cmdslen] = ';';
996 cmdslen++;
997 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -0200998
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -0200999 memcpy(cmds + cmdslen, str, len);
1000 cmdslen += len;
1001 cmds[cmdslen] = '\0';
1002 }
Lucas De Marchi28c175e2011-12-12 11:52:59 -02001003
Gustavo Sverzut Barbieribd3f5532011-12-10 20:47:01 -02001004 m->init.remove_commands = true;
1005 m->remove_commands = cmds;
1006 }
1007
1008 return mod->remove_commands;
1009
1010failed:
1011 ERR(mod->ctx, "out of memory\n");
1012 return NULL;
1013}