blob: 3acd5b0c95317052ef2b85006bbe8a37220744c1 [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;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020047 const char *path;
Lucas De Marchi7636e722011-12-01 17:56:03 -020048 struct kmod_list *dep;
Gustavo Sverzut Barbieri8d3f3ef2011-12-02 21:10:24 -020049 int refcount;
Lucas De Marchi7636e722011-12-01 17:56:03 -020050 struct {
51 bool dep : 1;
52 } init;
Lucas De Marchid753b8c2011-12-05 18:14:51 -020053 char name[];
Lucas De Marchi8f788d52011-11-25 01:22:56 -020054};
55
Lucas De Marchi6c343b12011-12-06 09:01:01 -020056static inline char *modname_normalize(char *modname, char buf[NAME_MAX],
57 size_t *len)
Lucas De Marchi8f788d52011-11-25 01:22:56 -020058{
Lucas De Marchi8f788d52011-11-25 01:22:56 -020059 char *c;
Lucas De Marchid753b8c2011-12-05 18:14:51 -020060 size_t s;
Lucas De Marchi8f788d52011-11-25 01:22:56 -020061
Lucas De Marchid753b8c2011-12-05 18:14:51 -020062 if (buf) {
63 buf[NAME_MAX] = '\0';
64 modname = strncpy(buf, modname, NAME_MAX - 1);
Lucas De Marchi8f788d52011-11-25 01:22:56 -020065 }
66
Lucas De Marchid753b8c2011-12-05 18:14:51 -020067 for (c = modname, s = 0; *c != '\0' && *c != '.'; c++) {
68 if (*c == '-')
69 *c = '_';
70 s++;
71 }
72
73 if (len)
74 *len = s;
75
Lucas De Marchi8f788d52011-11-25 01:22:56 -020076 *c = '\0';
Lucas De Marchid753b8c2011-12-05 18:14:51 -020077
Lucas De Marchi8f788d52011-11-25 01:22:56 -020078 return modname;
79}
80
Lucas De Marchi6c343b12011-12-06 09:01:01 -020081static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len)
82{
83 char *modname;
84
85 modname = basename(path);
86 if (modname == NULL || modname[0] == '\0')
87 return NULL;
88
89 return modname_normalize(modname, buf, len);
90}
91
Lucas De Marchi671d4892011-12-05 20:23:05 -020092int kmod_module_parse_depline(struct kmod_module *mod, char *line)
Lucas De Marchi7636e722011-12-01 17:56:03 -020093{
94 struct kmod_ctx *ctx = mod->ctx;
95 struct kmod_list *list = NULL;
96 char *p, *saveptr;
97 int err, n = 0;
98
99 assert(!mod->init.dep && mod->dep == NULL);
100 mod->init.dep = true;
101
102 p = strchr(line, ':');
103 if (p == NULL)
104 return 0;
105
Lucas De Marchi671d4892011-12-05 20:23:05 -0200106 *p = '\0';
107 if (mod->path == NULL)
108 mod->path = strdup(line);
109
Lucas De Marchi7636e722011-12-01 17:56:03 -0200110 p++;
111
112 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
113 p = strtok_r(NULL, " \t", &saveptr)) {
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200114 const char *modname = path_to_modname(p, NULL, NULL);
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200115 struct kmod_module *depmod;
Lucas De Marchi7636e722011-12-01 17:56:03 -0200116
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200117 err = kmod_module_new_from_name(ctx, modname, &depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200118 if (err < 0) {
119 ERR(ctx, "ctx=%p modname=%s error=%s\n",
120 ctx, modname, strerror(-err));
121 goto fail;
122 }
123
124 DBG(ctx, "add dep: %s\n", modname);
125
Lucas De Marchi1fc1c9a2011-12-02 10:00:03 -0200126 list = kmod_list_append(list, depmod);
Lucas De Marchi7636e722011-12-01 17:56:03 -0200127 n++;
128 }
129
130 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
131
132 mod->dep = list;
133 return n;
134
135fail:
136 kmod_module_unref_list(list);
137 mod->init.dep = false;
138 return err;
139}
140
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200141KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
142 const char *name,
143 struct kmod_module **mod)
144{
145 struct kmod_module *m;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200146 size_t namelen;
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200147 char name_norm[NAME_MAX];
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200148
149 if (ctx == NULL || name == NULL)
150 return -ENOENT;
151
Lucas De Marchi6c343b12011-12-06 09:01:01 -0200152 modname_normalize((char *)name, name_norm, &namelen);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200153
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200154 m = kmod_pool_get_module(ctx, name_norm);
155 if (m != NULL) {
156 *mod = kmod_module_ref(m);
157 return 0;
158 }
159
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200160 m = calloc(1, sizeof(*m) + namelen + 1);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200161 if (m == NULL) {
162 free(m);
163 return -ENOMEM;
164 }
165
166 m->ctx = kmod_ref(ctx);
Lucas De Marchi4f2bb7c2011-12-06 02:46:22 -0200167 memcpy(m->name, name_norm, namelen + 1);
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200168 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200169
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200170 kmod_pool_add_module(ctx, m);
171
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200172 *mod = m;
173
174 return 0;
175}
176
177KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
178 const char *path,
179 struct kmod_module **mod)
180{
181 struct kmod_module *m;
182 int err;
183 struct stat st;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200184 char name[NAME_MAX];
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200185 char *abspath;
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200186 size_t namelen;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200187
188 if (ctx == NULL || path == NULL)
189 return -ENOENT;
190
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200191 abspath = path_make_absolute_cwd(path);
192 if (abspath == NULL)
193 return -ENOMEM;
194
195 err = stat(abspath, &st);
196 if (err < 0) {
197 free(abspath);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200198 return -errno;
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200199 }
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200200
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200201 path_to_modname(path, name, &namelen);
202
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200203 m = kmod_pool_get_module(ctx, name);
204 if (m != NULL) {
Lucas De Marchi6bd0b8d2011-12-07 14:08:01 -0200205 if (m->path == NULL)
206 m->path = abspath;
207 else if (streq(m->path, abspath))
208 free(abspath);
209 else {
210 ERR(ctx, "kmod_module '%s' already exists with different path\n",
211 name);
212 free(abspath);
213 return -EEXIST;
214 }
215
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200216 *mod = kmod_module_ref(m);
217 return 0;
218 }
219
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200220 m = calloc(1, sizeof(*m) + namelen + 1);
221 if (m == NULL)
222 return -errno;
223
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200224 m->ctx = kmod_ref(ctx);
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200225 memcpy(m->name, name, namelen);
Lucas De Marchi71e975c2011-12-07 13:53:53 -0200226 m->path = abspath;
Gustavo Sverzut Barbieri87ca03b2011-12-04 12:34:02 -0200227 m->refcount = 1;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200228
Lucas De Marchifd186ae2011-12-06 03:38:37 -0200229 kmod_pool_add_module(ctx, m);
230
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200231 *mod = m;
232
233 return 0;
234}
235
236KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
237{
238 if (mod == NULL)
239 return NULL;
240
241 if (--mod->refcount > 0)
242 return mod;
243
244 DBG(mod->ctx, "kmod_module %p released\n", mod);
245
Lucas De Marchi7636e722011-12-01 17:56:03 -0200246 kmod_module_unref_list(mod->dep);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200247 kmod_unref(mod->ctx);
248 free((char *) mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200249 free(mod);
250 return NULL;
251}
252
253KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
254{
255 if (mod == NULL)
256 return NULL;
257
258 mod->refcount++;
259
260 return mod;
261}
262
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200263#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
264 do { \
265 if ((_err) < 0) \
266 goto _label_err; \
267 if (*(_list) != NULL) \
268 goto finish; \
269 } while (0)
270
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200271KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
272 const char *alias,
273 struct kmod_list **list)
274{
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200275 int err;
276
277 if (ctx == NULL || alias == NULL)
278 return -ENOENT;
279
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200280 if (list == NULL || *list != NULL) {
281 ERR(ctx, "An empty list is needed to create lookup\n");
282 return -ENOSYS;
283 }
284
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200285 /* Aliases from config file override all the others */
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200286 err = kmod_lookup_alias_from_config(ctx, alias, list);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200287 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200288
Lucas De Marchi64700e42011-12-01 15:57:53 -0200289 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
290 CHECK_ERR_AND_FINISH(err, fail, list, finish);
291
Lucas De Marchi9ba6f572011-11-30 20:31:45 -0200292 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
293 CHECK_ERR_AND_FINISH(err, fail, list, finish);
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200294
Lucas De Marchi49e61ca2011-12-01 16:27:04 -0200295 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
296 CHECK_ERR_AND_FINISH(err, fail, list, finish);
297
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200298finish:
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200299
300 return err;
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200301fail:
302 kmod_module_unref_list(*list);
303 *list = NULL;
Lucas De Marchi84f42202011-12-02 10:03:34 -0200304 return err;
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200305}
Lucas De Marchib14dcfd2011-11-30 20:29:51 -0200306#undef CHECK_ERR_AND_FINISH
307
Lucas De Marchi7f3eb0c2011-11-30 19:03:41 -0200308
309KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
310{
311 for (; list != NULL; list = kmod_list_remove(list))
312 kmod_module_unref(list->data);
313
314 return 0;
315}
316
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200317KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200318{
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200319 struct kmod_list *l, *l_new, *list_new = NULL;
320
321 if (mod == NULL)
322 return NULL;
323
Lucas De Marchi671d4892011-12-05 20:23:05 -0200324 if (!mod->init.dep) {
325 /* lazy init */
326 char *line = kmod_search_moddep(mod->ctx, mod->name);
327
328 if (line == NULL)
329 return NULL;
330
331 kmod_module_parse_depline((struct kmod_module *)mod, line);
332 free(line);
333
334 if (!mod->init.dep)
335 return NULL;
336 }
Lucas De Marchif1cd7992011-12-05 19:40:45 -0200337
338 kmod_list_foreach(l, mod->dep) {
339 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
340 if (l_new == NULL) {
341 kmod_module_unref(l->data);
342 goto fail;
343 }
344
345 list_new = l_new;
346 }
347
348 return list_new;
349
350fail:
351 ERR(mod->ctx, "out of memory\n");
352 kmod_module_unref_list(list_new);
353 return NULL;
Lucas De Marchi0835fc32011-12-01 20:06:08 -0200354}
355
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200356KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200357{
Gustavo Sverzut Barbieriad4d1ae2011-12-04 13:14:11 -0200358 if (entry == NULL)
359 return NULL;
360 return kmod_module_ref(entry->data);
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200361}
362
Gustavo Sverzut Barbieri69f9dd42011-12-04 14:02:30 -0200363KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
364{
365 // FIXME TODO: this should be available from /sys/module/foo
366 FILE *fp;
367 char line[4096];
368 int lineno = 0;
369 long size = -ENOENT;
370
371 if (mod == NULL)
372 return -ENOENT;
373
374 fp = fopen("/proc/modules", "r");
375 if (fp == NULL) {
376 int err = -errno;
377 ERR(mod->ctx,
378 "could not open /proc/modules: %s\n", strerror(errno));
379 return err;
380 }
381
382 while (fgets(line, sizeof(line), fp)) {
383 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
384 long value;
385
386 lineno++;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200387 if (tok == NULL || !streq(tok, mod->name))
Gustavo Sverzut Barbieri69f9dd42011-12-04 14:02:30 -0200388 continue;
389
390 tok = strtok_r(NULL, " \t", &saveptr);
391 if (tok == NULL) {
392 ERR(mod->ctx,
393 "invalid line format at /proc/modules:%d\n", lineno);
394 break;
395 }
396
397 value = strtol(tok, &endptr, 10);
398 if (endptr == tok || *endptr != '\0') {
399 ERR(mod->ctx,
400 "invalid line format at /proc/modules:%d\n", lineno);
401 break;
402 }
403
404 size = value;
405 break;
406 }
407 fclose(fp);
408 return size;
409}
410
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200411KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200412{
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200413 return mod->name;
414}
415
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200416/*
417 * Relative paths are relative to dirname. Absolute paths are only used when
418 * user created kmod_module by giving a path
419 */
Gustavo Sverzut Barbieri1ce08a52011-12-02 20:24:07 -0200420KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200421{
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200422 if (!mod->init.dep) {
423 /* lazy init */
424 char *line = kmod_search_moddep(mod->ctx, mod->name);
425
426 if (line == NULL)
427 return NULL;
428
429 kmod_module_parse_depline((struct kmod_module *) mod, line);
430 free(line);
431
432 if (!mod->init.dep)
433 return NULL;
434 }
435
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200436 return mod->path;
437}
438
439
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200440extern long delete_module(const char *name, unsigned int flags);
441
442KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
443 unsigned int flags)
444{
445 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200446
447 if (mod == NULL)
448 return -ENOENT;
449
450 /* Filter out other flags */
451 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
452
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200453 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200454 if (err != 0) {
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200455 ERR(mod->ctx, "Removing '%s': %s\n", mod->name,
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200456 strerror(-err));
457 return err;
458 }
459
460 return 0;
461}
462
463extern long init_module(void *mem, unsigned long len, const char *args);
464
465KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
466 unsigned int flags)
467{
468 int err;
469 void *mmaped_file;
470 struct stat st;
471 int fd;
472 const char *args = "";
473
474 if (mod == NULL)
475 return -ENOENT;
476
477 if (mod->path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200478 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200479 return -ENOSYS;
480 }
481
482 if (flags != 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200483 INFO(mod->ctx, "Flags are not implemented yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200484
485 if ((fd = open(mod->path, O_RDONLY)) < 0) {
486 err = -errno;
487 return err;
488 }
489
Lucas De Marchib418a822011-12-01 23:13:27 -0200490 fstat(fd, &st);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200491
492 if ((mmaped_file = mmap(0, st.st_size, PROT_READ,
493 MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
494 close(fd);
495 return -errno;
496 }
497
498 err = init_module(mmaped_file, st.st_size, args);
499 if (err < 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200500 ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200501
502 munmap(mmaped_file, st.st_size);
503 close(fd);
504
505 return err;
506}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200507
508KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
509{
510 switch (state) {
511 case KMOD_MODULE_BUILTIN:
512 return "builtin";
513 case KMOD_MODULE_LIVE:
514 return "live";
515 case KMOD_MODULE_COMING:
516 return "coming";
517 case KMOD_MODULE_GOING:
518 return "going";
519 default:
520 return NULL;
521 }
522}
523
524KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
525{
526 char path[PATH_MAX], buf[32];
527 int fd, err, pathlen;
528
529 pathlen = snprintf(path, sizeof(path),
530 "/sys/module/%s/initstate", mod->name);
531 fd = open(path, O_RDONLY);
532 if (fd < 0) {
533 err = -errno;
534
535 if (pathlen > (int)sizeof("/initstate") - 1) {
536 struct stat st;
537 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
538 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
539 return KMOD_MODULE_BUILTIN;
540 }
541
542 ERR(mod->ctx, "could not open '%s': %s\n",
543 path, strerror(-err));
544 return err;
545 }
546
547 err = read_str_safe(fd, buf, sizeof(buf));
548 close(fd);
549 if (err < 0) {
550 ERR(mod->ctx, "could not read from '%s': %s\n",
551 path, strerror(-err));
552 return err;
553 }
554
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200555 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200556 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200557 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200558 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200559 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200560 return KMOD_MODULE_GOING;
561
562 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
563 return -EINVAL;
564}
565
566KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
567{
568 char path[PATH_MAX];
569 long refcnt;
570 int fd, err;
571
572 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
573 fd = open(path, O_RDONLY);
574 if (fd < 0) {
575 err = -errno;
576 ERR(mod->ctx, "could not open '%s': %s\n",
577 path, strerror(errno));
578 return err;
579 }
580
581 err = read_str_long(fd, &refcnt, 10);
582 close(fd);
583 if (err < 0) {
584 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
585 path, strerror(-err));
586 return err;
587 }
588
589 return (int)refcnt;
590}
591
592KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
593{
594 char dname[PATH_MAX];
595 struct kmod_list *list = NULL;
596 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200597
598 if (mod == NULL)
599 return NULL;
600 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
601
602 d = opendir(dname);
603 if (d == NULL) {
604 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200605 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200606 return NULL;
607 }
608
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200609 for (;;) {
610 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200611 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200612 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200613 int err;
614
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200615 err = readdir_r(d, &de, &entp);
616 if (err != 0) {
617 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
618 mod->name, strerror(-err));
619 goto fail;
620 }
621
622 if (entp == NULL)
623 break;
624
625 if (de.d_name[0] == '.') {
626 if (de.d_name[1] == '\0' ||
627 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200628 continue;
629 }
630
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200631 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200632 if (err < 0) {
633 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200634 de.d_name, strerror(-err));
635 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200636 }
637
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200638 l = kmod_list_append(list, holder);
639 if (l != NULL) {
640 list = l;
641 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200642 ERR(mod->ctx, "out of memory\n");
643 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200644 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200645 }
646 }
647
648 closedir(d);
649 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200650
651fail:
652 closedir(d);
653 kmod_module_unref_list(list);
654 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200655}
656
657struct kmod_module_section {
658 unsigned long address;
659 char name[];
660};
661
662static void kmod_module_section_free(struct kmod_module_section *section)
663{
664 free(section);
665}
666
667KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
668{
669 char dname[PATH_MAX];
670 struct kmod_list *list = NULL;
671 DIR *d;
672 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200673
674 if (mod == NULL)
675 return NULL;
676 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
677
678 d = opendir(dname);
679 if (d == NULL) {
680 ERR(mod->ctx, "could not open '%s': %s\n",
681 dname, strerror(errno));
682 return NULL;
683 }
684
685 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200686
687 for (;;) {
688 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200689 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200690 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200691 unsigned long address;
692 size_t namesz;
693 int fd, err;
694
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200695 err = readdir_r(d, &de, &entp);
696 if (err != 0) {
697 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
698 mod->name, strerror(-err));
699 goto fail;
700 }
701
702 if (de.d_name[0] == '.') {
703 if (de.d_name[1] == '\0' ||
704 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200705 continue;
706 }
707
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200708 fd = openat(dfd, de.d_name, O_RDONLY);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200709 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200710 ERR(mod->ctx, "could not open '%s/%s': %m\n",
711 dname, de.d_name);
712 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200713 }
714
715 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200716 close(fd);
717
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200718 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200719 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
720 dname, de.d_name);
721 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200722 }
723
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200724 namesz = strlen(de.d_name) + 1;
725 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200726
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200727 if (section == NULL) {
728 ERR(mod->ctx, "out of memory\n");
729 goto fail;
730 }
731
732 section->address = address;
733 memcpy(section->name, de.d_name, namesz);
734
735 l = kmod_list_append(list, section);
736 if (l != NULL) {
737 list = l;
738 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200739 ERR(mod->ctx, "out of memory\n");
740 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200741 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200742 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200743 }
744
745 closedir(d);
746 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200747
748fail:
749 closedir(d);
750 kmod_module_unref_list(list);
751 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200752}
753
754KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
755{
756 struct kmod_module_section *section;
757 if (entry == NULL)
758 return NULL;
759 section = entry->data;
760 return section->name;
761}
762
763KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
764{
765 struct kmod_module_section *section;
766 if (entry == NULL)
767 return (unsigned long)-1;
768 section = entry->data;
769 return section->address;
770}
771
772KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
773{
774 while (list) {
775 kmod_module_section_free(list->data);
776 list = kmod_list_remove(list);
777 }
778}