blob: 401daaadee4aebeffbadfd997f4a3ea1d33a7bf5 [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 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);
Gustavo Sverzut Barbierif1fb6f82011-12-08 04:44:03 -0200248 free(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 Marchie005fac2011-12-08 10:42:34 -0200422 char *line;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200423
Lucas De Marchie005fac2011-12-08 10:42:34 -0200424 DBG(mod->ctx, "name='%s' path='%s'", mod->name, mod->path);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200425
Lucas De Marchie005fac2011-12-08 10:42:34 -0200426 if (mod->path != NULL)
427 return mod->path;
428 if (mod->init.dep)
429 return NULL;
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200430
Lucas De Marchie005fac2011-12-08 10:42:34 -0200431 /* lazy init */
432 line = kmod_search_moddep(mod->ctx, mod->name);
433 if (line == NULL)
434 return NULL;
435
436 kmod_module_parse_depline((struct kmod_module *) mod, line);
437 free(line);
Lucas De Marchic5e7b1f2011-12-05 20:28:13 -0200438
Lucas De Marchi6e869df2011-11-30 19:01:01 -0200439 return mod->path;
440}
441
442
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200443extern long delete_module(const char *name, unsigned int flags);
444
445KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
446 unsigned int flags)
447{
448 int err;
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200449
450 if (mod == NULL)
451 return -ENOENT;
452
453 /* Filter out other flags */
454 flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
455
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200456 err = delete_module(mod->name, flags);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200457 if (err != 0) {
Lucas De Marchid753b8c2011-12-05 18:14:51 -0200458 ERR(mod->ctx, "Removing '%s': %s\n", mod->name,
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200459 strerror(-err));
460 return err;
461 }
462
463 return 0;
464}
465
466extern long init_module(void *mem, unsigned long len, const char *args);
467
468KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
469 unsigned int flags)
470{
471 int err;
472 void *mmaped_file;
473 struct stat st;
474 int fd;
475 const char *args = "";
476
477 if (mod == NULL)
478 return -ENOENT;
479
480 if (mod->path == NULL) {
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200481 ERR(mod->ctx, "Not supported to load a module by name yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200482 return -ENOSYS;
483 }
484
485 if (flags != 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200486 INFO(mod->ctx, "Flags are not implemented yet\n");
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200487
488 if ((fd = open(mod->path, O_RDONLY)) < 0) {
489 err = -errno;
490 return err;
491 }
492
Lucas De Marchib418a822011-12-01 23:13:27 -0200493 fstat(fd, &st);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200494
495 if ((mmaped_file = mmap(0, st.st_size, PROT_READ,
496 MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
497 close(fd);
498 return -errno;
499 }
500
501 err = init_module(mmaped_file, st.st_size, args);
502 if (err < 0)
Lucas De Marchi1b2e26a2011-11-25 01:28:39 -0200503 ERR(mod->ctx, "Failed to insert module '%s'\n", mod->path);
Lucas De Marchi8f788d52011-11-25 01:22:56 -0200504
505 munmap(mmaped_file, st.st_size);
506 close(fd);
507
508 return err;
509}
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200510
511KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
512{
513 switch (state) {
514 case KMOD_MODULE_BUILTIN:
515 return "builtin";
516 case KMOD_MODULE_LIVE:
517 return "live";
518 case KMOD_MODULE_COMING:
519 return "coming";
520 case KMOD_MODULE_GOING:
521 return "going";
522 default:
523 return NULL;
524 }
525}
526
527KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
528{
529 char path[PATH_MAX], buf[32];
530 int fd, err, pathlen;
531
532 pathlen = snprintf(path, sizeof(path),
533 "/sys/module/%s/initstate", mod->name);
534 fd = open(path, O_RDONLY);
535 if (fd < 0) {
536 err = -errno;
537
538 if (pathlen > (int)sizeof("/initstate") - 1) {
539 struct stat st;
540 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
541 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
542 return KMOD_MODULE_BUILTIN;
543 }
544
545 ERR(mod->ctx, "could not open '%s': %s\n",
546 path, strerror(-err));
547 return err;
548 }
549
550 err = read_str_safe(fd, buf, sizeof(buf));
551 close(fd);
552 if (err < 0) {
553 ERR(mod->ctx, "could not read from '%s': %s\n",
554 path, strerror(-err));
555 return err;
556 }
557
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200558 if (streq(buf, "live\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200559 return KMOD_MODULE_LIVE;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200560 else if (streq(buf, "coming\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200561 return KMOD_MODULE_COMING;
Lucas De Marchi877e80c2011-12-07 02:26:31 -0200562 else if (streq(buf, "going\n"))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200563 return KMOD_MODULE_GOING;
564
565 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
566 return -EINVAL;
567}
568
569KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
570{
571 char path[PATH_MAX];
572 long refcnt;
573 int fd, err;
574
575 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
576 fd = open(path, O_RDONLY);
577 if (fd < 0) {
578 err = -errno;
579 ERR(mod->ctx, "could not open '%s': %s\n",
580 path, strerror(errno));
581 return err;
582 }
583
584 err = read_str_long(fd, &refcnt, 10);
585 close(fd);
586 if (err < 0) {
587 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
588 path, strerror(-err));
589 return err;
590 }
591
592 return (int)refcnt;
593}
594
595KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
596{
597 char dname[PATH_MAX];
598 struct kmod_list *list = NULL;
599 DIR *d;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200600
601 if (mod == NULL)
602 return NULL;
603 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
604
605 d = opendir(dname);
606 if (d == NULL) {
607 ERR(mod->ctx, "could not open '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200608 dname, strerror(errno));
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200609 return NULL;
610 }
611
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200612 for (;;) {
613 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200614 struct kmod_module *holder;
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200615 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200616 int err;
617
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200618 err = readdir_r(d, &de, &entp);
619 if (err != 0) {
620 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
621 mod->name, strerror(-err));
622 goto fail;
623 }
624
625 if (entp == NULL)
626 break;
627
628 if (de.d_name[0] == '.') {
629 if (de.d_name[1] == '\0' ||
630 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200631 continue;
632 }
633
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200634 err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200635 if (err < 0) {
636 ERR(mod->ctx, "could not create module for '%s': %s\n",
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200637 de.d_name, strerror(-err));
638 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200639 }
640
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200641 l = kmod_list_append(list, holder);
642 if (l != NULL) {
643 list = l;
644 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200645 ERR(mod->ctx, "out of memory\n");
646 kmod_module_unref(holder);
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200647 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200648 }
649 }
650
651 closedir(d);
652 return list;
Lucas De Marchi53886dd2011-12-05 13:24:23 -0200653
654fail:
655 closedir(d);
656 kmod_module_unref_list(list);
657 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200658}
659
660struct kmod_module_section {
661 unsigned long address;
662 char name[];
663};
664
665static void kmod_module_section_free(struct kmod_module_section *section)
666{
667 free(section);
668}
669
670KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
671{
672 char dname[PATH_MAX];
673 struct kmod_list *list = NULL;
674 DIR *d;
675 int dfd;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200676
677 if (mod == NULL)
678 return NULL;
679 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
680
681 d = opendir(dname);
682 if (d == NULL) {
683 ERR(mod->ctx, "could not open '%s': %s\n",
684 dname, strerror(errno));
685 return NULL;
686 }
687
688 dfd = dirfd(d);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200689
690 for (;;) {
691 struct dirent de, *entp;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200692 struct kmod_module_section *section;
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200693 struct kmod_list *l;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200694 unsigned long address;
695 size_t namesz;
696 int fd, err;
697
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200698 err = readdir_r(d, &de, &entp);
699 if (err != 0) {
700 ERR(mod->ctx, "could not iterate for module '%s': %s\n",
701 mod->name, strerror(-err));
702 goto fail;
703 }
704
705 if (de.d_name[0] == '.') {
706 if (de.d_name[1] == '\0' ||
707 (de.d_name[1] == '.' && de.d_name[2] == '\0'))
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200708 continue;
709 }
710
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200711 fd = openat(dfd, de.d_name, O_RDONLY);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200712 if (fd < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200713 ERR(mod->ctx, "could not open '%s/%s': %m\n",
714 dname, de.d_name);
715 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200716 }
717
718 err = read_str_ulong(fd, &address, 16);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200719 close(fd);
720
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200721 if (err < 0) {
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200722 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
723 dname, de.d_name);
724 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200725 }
726
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200727 namesz = strlen(de.d_name) + 1;
728 section = malloc(sizeof(*section) + namesz);
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200729
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200730 if (section == NULL) {
731 ERR(mod->ctx, "out of memory\n");
732 goto fail;
733 }
734
735 section->address = address;
736 memcpy(section->name, de.d_name, namesz);
737
738 l = kmod_list_append(list, section);
739 if (l != NULL) {
740 list = l;
741 } else {
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200742 ERR(mod->ctx, "out of memory\n");
743 free(section);
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200744 goto fail;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200745 }
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200746 }
747
748 closedir(d);
749 return list;
Lucas De Marchi40923bd2011-12-05 13:40:16 -0200750
751fail:
752 closedir(d);
753 kmod_module_unref_list(list);
754 return NULL;
Gustavo Sverzut Barbierif12ae3c2011-12-04 12:40:00 -0200755}
756
757KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
758{
759 struct kmod_module_section *section;
760 if (entry == NULL)
761 return NULL;
762 section = entry->data;
763 return section->name;
764}
765
766KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
767{
768 struct kmod_module_section *section;
769 if (entry == NULL)
770 return (unsigned long)-1;
771 section = entry->data;
772 return section->address;
773}
774
775KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
776{
777 while (list) {
778 kmod_module_section_free(list->data);
779 list = kmod_list_remove(list);
780 }
781}